·首页 ·asp ·.net ·php ·JSP ·CGI ·数据库 ·网页设计 ·网管专栏 ·XML ·工具软件 ·办公软件 ·操作系统 ·程序设计 ·LINUX 
  当前位置: 普克>>计算机教程>>.net>>面向对象编程>>使用OleDbCommand对象更新S
flash视频教学

photoshop专题

asp.net专题

office专题

使用OleDbCommand对象更新SQL Server中的二进制文件


面向对象编程 发表时间:2006-4-8 字体:  返回
使用OleDbCommand对象更新SQL Server中的二进制文件



作者 朱二


利用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 );
}
}
}



上一篇:一份很有价值的子类化的源代码!
下一篇:从MP3中提取歌曲信息

普克创业投资网刊载此文不代表同意其说法或描述,仅为提供更多信息。
在百度中搜索使用OleDbCommand对象更新SQL Server中的二进制文件的相关内容]   [在狗狗中搜索使用OleDbCommand对象更新SQL Server中的二进制文件的相关内容]
Copyright @ 2006 PUPK.COM 普克创业投资网 版权所有
 建议使用1024*768以达到最好的浏览效果