How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
Create the Project
1.
Add a table named MyImages to your SQL Server Northwind database. Include the following fields in your table:
•
Identity field that is named "ID" of type Int.
•
Field that is named "Description" of type VarChar with a length of 50.
•
Field that is named "ImgField" of type Image.
2.
Start Visual Studio .NET, and then create a new Visual C# Windows Application project.
3.
Drag two Button controls from the toolbox to the default form, Form1.
4.
In the Properties window, change the Text property of Button1 to Save to Database (from File), and then change the Text property of Button2 to Save to File (from Database).
5.
Add the following code to the top of the Code window:
using System.Data; using System.Data.SqlClient; using System.IO;
6.
Double-click Button1, and then add the following code to the Button1_Click event handler.
Note Uid <user name> must have permissions to perform these operations on the database.
{ SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;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"); con.Close(); }
7.
Double-click Button2, and then add the following code to the Button2_Click event handler.
Note Uid <user name> must have permissions to perform these operations on the database.
{ SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;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(); }
8.
Press F5 to compile and to run the application.
9.
Click Save to Database (from File) to load the image, C:\WinNT\Gone Fishing.bmp, into the SQL Server Image field.
10.
Click Save to File (from Database) to save the data from the SQL Server Image field back to a file.
'C#.NET DB' 카테고리의 다른 글
[MS/세미나동영상]데이터베이스 보안전략 (0) | 2011.09.01 |
---|---|
[TableAdapter / DataSet] 초간단 데이터셋 사용하기 (1) | 2011.02.16 |
[PROCEDURE / NextResult ] 프로시저로 사용자정보 한번에 가지고 오기 (0) | 2010.11.11 |
[MSDTC/TransactionScope] DTC의 네트워크 액세스를 활성화 (0) | 2010.07.21 |
[DataSet]데이터셋 사용법 (0) | 2010.01.12 |
[ SqlDataSource , FilterExpression ] 초간단 데이터검색방법 , 필터링 사용 설정 (0) | 2009.12.15 |
[DataTable] 데이타테이블 간단한 사용법 및 검색방법 (0) | 2009.12.15 |
[DataSet]간단한 DataAdapter와 DataSet 만들기 (0) | 2009.12.15 |
[OleDbDataAdapter] 업데이트 , 삭제 , 셀렉트 속성 (0) | 2009.12.15 |
[SqlTransaction ] ADO.NET 수동 트랜잭션을 코딩하는 방법 (0) | 2009.12.15 |