----------------------------------------------------------- New Message on cochindotnet
----------------------------------------------------------- From: sajay Message 2 in Discussion -----Original Message----- From: Vimal Ghosh [mailto:[EMAIL PROTECTED] Subject: Tip of the Day...(ASP.NET) Read and Write BLOB Data by Using ADO.NET Through ASP.NET 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: 1. Start Visual Studio NET. 2. Drag two Button controls from the Web Forms section of the toolbox to the default Web form. 3. 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 4. Double-click Button1, and then add the following code to the Button1_Click event handler: Dim con As New SqlConnection("Server=urserver;uid=uid;pwd=urpassword;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") 5. 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 6. Press F5 to compile and to run the application. 7. 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. 8. Click Load File from Database to load the data from the SQL Server Image field to the Web browser. Thanks, Vimal See the next tip to get info using Upload Control ----------------------------------------------------------- To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings. http://groups.msn.com/cochindotnet/_emailsettings.msnw Need help? If you've forgotten your password, please go to Passport Member Services. http://groups.msn.com/_passportredir.msnw?ppmprop=help For other questions or feedback, go to our Contact Us page. http://groups.msn.com/contact If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list. mailto:[EMAIL PROTECTED]
