How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
来源:互联网 发布:linux lnmp搭建 编辑:程序博客网 时间:2024/04/30 12:28
SUMMARY
The GetChunk and the AppendChunk methods are not available in ADO.NET on DataReader columns, DataSet columns, or Command parameters. This article describes how to use Visual C# .NET to read and write binary large object (BLOB) fields.
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:
Note Uid <user name> must have permissions to perform these operations on the database.
Note Uid <user name> must have permissions to perform these operations on the database.
Back to the top
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 ServerBack to the top
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.Back to the top
APPLIES TO
•Microsoft ADO.NET 1.1•Microsoft ADO.NET 1.0•Microsoft Visual C# .NET 2003 Standard Edition•Microsoft Visual C# .NET 2002 Standard Edition- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How To Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
- How to send raw data to a printer by using Visual C# .NET
- How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
- How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
- HOW TO: Transfer Data to an Excel Workbook by Using Visual Basic .NET
- How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
- How to write ANSI C programs by using Visual C++
- Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual
- How To Read and Write XML Documents with GDataXML
- How To Read and Write XML Documents with GDataXML
- How To Read and Write XML Documents with GDataXML
- HOW TO:使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- HOW TO:使用 Visual C# .NET 在 ADO.NET 中以编程方式构建连接字符串
- 自动生成 SQL* LOADER 的控制文件
- 学车记(二)
- 学java也是个游戏
- .net打包自动安装数据库!
- 资源网
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Move the Item of ListBox
- 盘点西甲之巴萨篇
- 巴萨两点最大的隐忧
- 游戏童年之打水漂
- 反编译类
- 顺便记录一下groupwise使用gaim
- [转载]开发工程师人生之路
- 无话可说