飛艷小屋

          程序--人生--哲學(xué)___________________歡迎艷兒的加入

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            52 Posts :: 175 Stories :: 107 Comments :: 0 Trackbacks

          C#中通過(guò)使用ADO.NET讀寫(xiě)B(tài)LOB數(shù)據(jù)

          [日期:2005-02-12] [字體: ]
          本文引用下面的 Microsoft .NET 框架類(lèi)庫(kù)名稱(chēng)空間:
          ? System.Data.SqlClient
          ? System.IO

          本任務(wù)的內(nèi)容

          ? 概要
           
          ? 要求
          ? 創(chuàng)建項(xiàng)目

          概要

          在 ADO.NET 中,DataReader 列、DataSet 列或 Command 參數(shù)不能使用 GetChunk AppendChunk 方法。本文介紹如何使用 Visual C# .NET 讀寫(xiě)二進(jìn)制大對(duì)象 (BLOB) 字段。

          返回頁(yè)首

          要求

          下面的列表列出了推薦使用的硬件、軟件、網(wǎng)絡(luò)結(jié)構(gòu)以及所需的 Service Pack:
          ? Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server
          ? Microsoft Visual Studio .NET
          ? Microsoft SQL Server
          返回頁(yè)首

          創(chuàng)建項(xiàng)目

          1. 在您的 SQL Server 羅斯文數(shù)據(jù)庫(kù)中添加一個(gè)名為 MyImages 的表。在該表中包含以下字段:
          ? 標(biāo)識(shí)字段,名為"ID",類(lèi)型為 Int
          ? 字段,名為"Description",類(lèi)型為 VarChar,長(zhǎng)度為 50。
          ? 字段,名為"ImgField",類(lèi)型為 Image

          2. 啟動(dòng) Visual Studio .NET,然后新建一個(gè) Visual C# Windows 應(yīng)用程序項(xiàng)目。
          3. 將兩個(gè) Button 控件從工具箱拖到默認(rèn)窗體 Form1 上。
          4. 在"屬性"窗口中,將 Button1Text 屬性更改為保存到數(shù)據(jù)庫(kù)(從文件),將 Button2Text 屬性更改為保存到文件(從數(shù)據(jù)庫(kù))
          5. 將下面的代碼添加到"代碼"窗口頂部:
          using System.Data;
          using System.Data.SqlClient;
          using System.IO;
          6. 雙擊 Button1,然后將以下代碼添加到 Button1_Click 事件處理程序中:
          {
          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");
          
          con.Close();
          
          }
          7. 雙擊 Button2,然后將以下代碼添加到 Button2_Click 事件處理程序中:
          {
          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();
          }
          8. 按 F5 鍵編譯并運(yùn)行該應(yīng)用程序。
          9. 單擊"保存到數(shù)據(jù)庫(kù)(從文件)",將位于 C:\WinNT\Gone Fishing.bmp 的圖像加載到 SQL Server Image 字段。
          10. 單擊"保存到文件(從數(shù)據(jù)庫(kù))",將 SQL Server Image 字段的數(shù)據(jù)保存回文件中。
          posted on 2005-11-17 17:03 天外飛仙 閱讀(991) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): .net
          主站蜘蛛池模板: 文昌市| 肃北| 墨竹工卡县| 尼勒克县| 杭州市| 乌兰浩特市| 松阳县| 肥城市| 新乐市| 高安市| 儋州市| 前郭尔| 平乐县| 北流市| 平南县| 四子王旗| 太原市| 行唐县| 吐鲁番市| 宽城| 永昌县| 淮滨县| 绥棱县| 灵武市| 武义县| 陵川县| 石河子市| 洛宁县| 博湖县| 天门市| 开远市| 永泰县| 曲麻莱县| 石台县| 泸西县| 廊坊市| 建瓯市| 台州市| 东明县| 遂宁市| 桂林市|