How to: In Visual C # .NET reads and writes BLOB data using ADO.NET (from MSDN)

xiaoxiao2021-03-06  59

The release number of this article has been CHS309158

For Microsoft Visual Basic .NET versions of this article, see

308042.

For Microsoft Visual J # .NET versions of this article, see

320629.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Data.sqlclient system.io

This task content

summary

Create a project

Overview in ADO.NET,

DataReader column,

DataSet column or

Command parameters cannot be used

Getchunk and

Appendchunk method. This article describes how to read and write a binary large object (BLOB) field using Visual C # .NET.

Back to top

Require the following list lists the recommended hardware, software, network structure, and service pack 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

Back to top

Create a project

Add a table named myImages in your SQL Server Ross Wen database. In this table contains the following fields:

Identify field, named "ID", type INT. Field, named "Description", type VARCHAR, with a length of 50. Field, named "imgfield", type Image. Start Visual Studio .NET and create a Visual C # Windows application project. Drag two Button controls from the toolbox to the default form FORM1. In the Properties window, change the Text property of Button1 to the database (from file), change the button of Button2 to save to the file (from the database). Add the following code to the Code window: use system.data;

Using system.data.sqlclient;

Using system.io; Double-click Button1, then add the following code to the Button1_Click event handler: {

SqlConnection Con = New SqlConnection ("Server = Darkover; UID = SA; PWD = Password1; Database = northwind");

SqlDataAdapter Da = New SqldataAdapter ("Select * from MyImages", CON;

Sqlcommandbuilder mycb = new sqlcommandbuilder (da);

DataSet DS = New Dataset ("MyImages");

Da.MissingSchemaAction = missingschemaaction.addwithkey;

FILESTREAM FS = New FileStream (@ "C: / Winnt / Gone Fishing.bmp", FileMode.Openorcreate, FileAccess.Read;

Byte [] mydata = new byte [fs.length];

fs.read (MyData, 0, System.Convert.Toint32 (fs.length)); fs.close ();

Da.fill (DS, "MyImages");

DataRow myrow;

Myrow = ds.tables ["MyImages"]. newrow ();

Myrow ["description"] = "this would be description text";

Myrow ["IMGFIELD"] = MyData;

DS.TABLES ["MyImages"]. Rows.Add (MyRow);

Da.Update (DS, "MyImages");

C. close ();

} Double-click Button2 and add the following code to the Button2_Click event handler: {

SqlConnection Con = New SqlConnection ("Server = Darkover; UID = SA; PWD = Password1; Database = northwind");

SqlDataAdapter Da = New SqldataAdapter ("Select * from MyImages", CON;

Sqlcommandbuilder mycb = new sqlcommandbuilder (da);

DataSet DS = New Dataset ("MyImages");

BYTE [] mydata = new byte [0];

Da.fill (DS, "MyImages");

DataRow myrow;

Myrow = ds.tables ["MyImages"]. Rows [0];

MyData = (byte []) Myrow ["IMGFIELD"];

INT Arraysize = new int ();

Arraysize = mydata.getupperbound (0);

FILESTREAM FS = New FileStream (@ "c: / winnt / gone fishing2.bmp", filemode.openorcreate, fileaccess.write;

fs.write (MyData, 0, Arraysize);

fs.close ();

} Press F5 to compile and run the application. Click "Save to Database (From File)", load the image of C: / Winnt / Gone Fishing.bmp to the SQL Server Image field. Click "Save to File (from Database)", save the data of the SQL Server Image field back to the file.

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)

Recent Updated: 2002-6-18 (1.0) Keyword Kbhowto KbhowTomaster Kbio Kbsqlclient KbsystemData KB309158

转载请注明原文地址:https://www.9cbs.com/read-109041.html

New Post(0)