Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:- Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
- Microsoft SQL Server
Create the Project
This sample project connects to the SQL Server Pubs database. This project uses the existing Pub_info table and stores a .jpg file in the logo field of this table. The data type of the logo field is Image.- To create a new ASP.NET Web Application project in Microsoft Visual Basic .NET, follow these steps:
- Start Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
- Drag two Button controls from the Web Forms section of the toolbox to the default Web form.
- In the Properties window, change the Text property of Button1 to Save File to Database, and then change the Text property of Button2 to Load File from Database.
- Add the following code to the top of the code-behind window:
Imports System.Data.SqlClient Imports System.IO
- Double-click Button1, and then add the following code to the Button1_Click event handler:
Dim con As New SqlConnection("Server=yileiw2;uid=sqlauth;pwd=sqlauth;database=pubs") Dim da As New SqlDataAdapter("Select * From pub_info", con) Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() da.MissingSchemaAction = MissingSchemaAction.AddWithKey con.Open() da.Fill(ds, "Pub_info") Dim fs As New FileStream _ ("C:\SomePath\MyPhoto.jpg", FileMode.OpenOrCreate, _ FileAccess.Read) Dim MyData(fs.Length) As Byte fs.Read(MyData, 0, fs.Length) fs.Close() ds.Tables("Pub_info").Rows(0)("logo") = MyData da.Update(ds, "Pub_info") fs = Nothing MyCB = Nothing ds = Nothing da = Nothing con.Close() con = Nothing Response.Write("File saved to database")
- Double-click Button2, and then add the following code to the Button2_Click event handler:
Dim con As New SqlConnection("Server=yileiw2;uid=sqlauth;pwd=sqlauth;database=pubs") Dim da As New SqlDataAdapter("Select * From pub_info", con) Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() con.Open() da.Fill(ds, "Pub_info") Dim myRow As DataRow myRow = ds.Tables("Pub_info").Rows(0) Dim MyData() As Byte MyData = myRow("logo") Response.Buffer = True Response.ContentType = "Image/JPEG" Response.BinaryWrite(MyData) MyCB = Nothing ds = Nothing da = Nothing con.Close() con = Nothing
- Press F5 to compile and to run the application.
- Click Save File to Database to save MyPhoto.jpg in the SQL Server Image field. After you receive the confirmation message that the image has been saved, check your table to verify.
- Click Load File from Database to load the data from the SQL Server Image field to the Web browser.
Additional Information
Although this sample uses a .jpg file, you can download any type of file from your Web application to the client browser. For example, to use a Microsoft Word document instead of a .jpg file, replace the following code
Response.ContentType = "Image/JPEG"
Response.AddHeader("Content-Disposition", "attachment;filename=blob.doc")
Response.ContentType = "application/msword"
Active server pages - ContentType
http://www.asptutorial.info/sscript/ContentType.asp
http://www.asptutorial.info/sscript/ContentType.asp