注册
 找回密码
 注册
江西广告网
查看: 326|回复: 0
打印 上一主题 下一主题

用OleDbCommand更新SQL Server的二进制文件

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-1-22 16:54:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
  利用ADO.NET中的OleDbConnection\OleDbCommand 可以方便的对SQL Server中的二进制文件进行更新操作,下面是详细的代码演示      演示环境:   数据库机器名 :s_test   登陆名 :sa   密码 :7890   数据库名 db_test      下面建立一个表:    create table tb_test(id int identity(1,1),photo image ,constraint pk_tb_test primary key(id))       一、将硬盘上的文件保存至数据库(C#)   //----------------------------------------------------------   //----------------------------------------------------------   //下面的示例将c:\1.txt文件保存至数据库的tb_test表中   //----------------------------------------------------------   //----------------------------------------------------------      using System;   using System.IO;?   using System.Data;   using System.Data.OleDb;      class image_test   {   [STAThread]   static void Main(string[] args)   {   try   {   //初始化OleDbConnection和OleDbCommand   OleDbConnection cn = new OleDbConnection("provider=sqloledb;server=s_test;user id=sa;password=7890;initial catalog=db_test");   OleDbCommand cmd = new OleDbCommand("INSERT tb_test(photo) VALUES(?)",cn);      //打开文件   FileStream fs = new FileStream("c:\\1.txt", FileMode.Open, FileAccess.Read);   Byte[] b = new Byte[fs.Length];   fs.Read(b, 0, b.Length);   fs.Close();      //打开连接   OleDbParameter prm = new OleDbParameter("@photo",OleDbType.VarBinary ,b.Length,?   ParameterDirection.Input, false, 0, 0, null,DataRowVersion.Current, b);   cmd.Parameters.Add(prm);   cn.Open();      //执行   if (cmd.ExecuteNonQuery() == 1)   Console.WriteLine("OK");   else   Console.WriteLine("Fail");?   cn.Close();   }   catch(Exception ex)   {   Console.WriteLine(ex.Message );   }   }   }?      二、更新数据库中保存的文件   //----------------------------------------------------------   //----------------------------------------------------------   //下面的示例用将数据库的tb_test表中ID=1的记录的photo更新为c:\1.txt   //----------------------------------------------------------   //----------------------------------------------------------      using System;   using System.IO;?   using System.Data;   using System.Data.OleDb;      class image_test   {   [STAThread]   static void Main(string[] args)   {   try   {   //初始化OleDbConnection和OleDbCommand   OleDbConnection cn = new OleDbConnection("provider=sqloledb;server=s_test;user id=sa;password=7890;initial catalog=db_test");   OleDbCommand cmd = new OleDbCommand("UPDATE tb_test SET photo= ? WHERE ID=1",cn);      //打开文件   FileStream fs = new FileStream("c:\\1.txt", FileMode.Open, FileAccess.Read);   Byte[] b = new Byte[fs.Length];   fs.Read(b, 0, b.Length);   fs.Close();      //打开连接   OleDbParameter prm = new OleDbParameter("@photo",OleDbType.VarBinary ,b.Length,?   ParameterDirection.Input, false, 0, 0, null,DataRowVersion.Current, b);   cmd.Parameters.Add(prm);   cn.Open();      //执行   if (cmd.ExecuteNonQuery() == 1)   Console.WriteLine("OK");   else   Console.WriteLine("Fail");?   cn.Close();   }   catch(Exception ex)   {   Console.WriteLine(ex.Message );   }   }   }  <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表