???? ID: 309158 - ????? ???????: 29 ??????? 2010 - ??????: 4.0

How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

TheGetChunk??AppendChunkmethods are not available in ADO.NET onDataReadercolumns,DataSetcolumns, orCommandparameters. This article describes how to use Visual C# .NET to read and write binary large object (BLOB) fields.

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ?????? ?? ?????? ??? ?? ???? ???:
  • Microsoft Windows 2000 Professional ???, Windows 2000 ?????, Windows 2000 ?????, ?? Windows NT 4.0 ????? ????? ????
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server

Create the Project

  1. Add a table named MyImages to your SQL Server?????????database. Include the following fields in your table:
    • ?? ?????? ?? "ID" ??? ?? ????? ?????Int.
    • ????? ?????? ?? "?????" ?? ???VarChar?? 50 ?? ??????
    • ????? ?????? ?? "ImgField" ?? ??????.

  2. Visual Studio .NET ??? ??????? ????, ?? ?? ????? C# Windows ????????? ??? ??? ????????? ??????
  3. ?? ????????????????? ????? ????? ?? ??????? ???????, Form1 ?? ????
  4. ??? ????? ???, ??? ?????????? ???Button1???? ?? ???(?????) ?? ??????? ??? ??????, ?? ?? ????????? ????????? ???Button2???? ?? ???(?? ???????) ???? ??? ??????.
  5. ??? ????? ?? ???? ??? ??? ????? ??? ??????:
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    					
  6. ???-????? ????Button1, ?? ???? ??? ????? ??? ??????Button1_Click????? ???????

    ???:Uid <user name="">?? ?? ???????? ???? ?? ??? ?????? ???? ?????? ??</user> ????????
    {
    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. ???-????? ????Button2, ?? ???? ??? ????? ??? ??????Button2_Click????? ???????

    ???:Uid <user name="">?? ?? ???????? ???? ?? ??? ?????? ???? ?????? ??</user> ????????
    {
    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. ?????? ???? ?? ????????? ?? ????? ?? ??? F5 ??????
  9. ????? ????,(?????) ?? ??????? ??? ??????SQL ????? ??? ???, C:\WinNT\Gone Fishing.bmp, ?? ??? ???? ?? ??????????? ???
  10. ????? ????,(?? ???????) ???? ??? ??????SQL ????? ?? ???? ?? ?????? ?? ?????????? ????? ?? ????? ???

???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbhowtomaster kbio kbsqlclient kbsystemdata kbmt KB309158 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:309158  (http://support.microsoft.com/kb/309158/en-us/ )