如何使用C#和ADO.NET在SQL Server数据库读取和写入blob数据
2012-11-20 11:03
846 查看
转自:http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
标题:How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
The GetChunk and
the AppendChunk methods
are not available in ADO.NET on DataReader columns, DataSet columns,
orCommand parameters.
This article describes how to use Visual C# .NET to read and write binary large object (BLOB) fields.
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
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.
Start Visual Studio .NET, and then create a new 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 Save to Database (from File), and then change the Textproperty of Button2 to Save
to File (from Database).
Add the following code to the top of the Code window:
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.
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.
Press F5 to compile and to run the application.
Click Save to Database (from File) to load the image, C:\WinNT\Gone Fishing.bmp, into the SQL Server Image field.
Click Save to File (from Database) to save the data from the SQL Server Image field back to a file.
标题:How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
The GetChunk and
the AppendChunk methods
are not available in ADO.NET on DataReader columns, DataSet columns,
orCommand parameters.
This article describes how to use Visual C# .NET to read and write binary large object (BLOB) fields.
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
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.
Start Visual Studio .NET, and then create a new 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 Save to Database (from File), and then change the Textproperty of Button2 to Save
to File (from Database).
Add the following code to the top of the Code window:
using System.Data; using System.Data.SqlClient; using System.IO;
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.T 4000 oInt32(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(); }
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(); }
Press F5 to compile and to run the application.
Click Save to Database (from File) to load the image, C:\WinNT\Gone Fishing.bmp, into the SQL Server Image field.
Click Save to File (from Database) to save the data from the SQL Server Image field back to a file.
相关文章推荐
- 如何读取和写入文件或 BLOB 列使用 ADO.NET 和 Visual C#.NET
- 使用ADO.NET 和C# 处理BLOB 数据
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- 使用ADO.NET 和C# 处理BLOB 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- C#中通过使用ADO.NET读写BLOB数据
- ADO.NET 2.0:如何排除错误信息「当目前没有数据时,尝试读取无效」(C#)
- ADO.NET 2.0:如何排除错误信息「当目前没有数据时,尝试读取无效」(C#)
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- [翻译]Visual C# .NET中使用ADO.NET读写 BLOB 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用ADO.NET 和C# 处理BLOB 数据
- 使用ADO.NET 和C# 处理BLOB 数据
- C#程序中:如何向xml文件中写入数据和读取数据
- 如何:使用 ADO.NET 实体框架数据源创建数据服务(WCF 数据服务)
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel