使用OleDbCommand对象更新SQL Server中的二进制文件
来源:互联网 发布:淘宝超级会员大红包 编辑:程序博客网 时间:2024/06/05 19:10
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
使用OleDbCommand对象更新SQL Server中的二进制文件
作者 朱二 利用ADO.NET中的OleDbConnectionOleDbCommand 可以方便的对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和OleDbCommandOleDbConnection 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");elseConsole.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和OleDbCommandOleDbConnection 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");elseConsole.WriteLine("Fail");?cn.Close();}catch(Exception ex){Console.WriteLine(ex.Message );}}} <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 使用OleDbCommand对象更新SQL Server中的二进制文件
- 使用OleDbCommand对象更新SQL Server中的二进制文件
- 利用DataSet存取SQL Server中的二进制文件
- 利用DataSet存取SQL Server中的二进制文件
- OleDBConnection连接SQL Server表时,OleDbCommand在使用Update语句时,使用参数出错
- 关于利用DataSet存取SQL Server中的二进制文件
- Hibernate读写SQL-Server二进制文件
- SQL Server中的对象名称
- 第一章、Sql server 中的对象
- 细说Sql Server中的视图(上)(更新)
- 细说Sql Server中的视图(上)(更新)
- 细说Sql Server中的视图(上)(更新)
- 如何在 Visual C# .NET 中使用 SqlDataAdapter 对象更新 SQL Server 数据库
- sql server 中的使用技巧
- SQL Server 中的触发器使用
- SQL SERVER中的分区表使用
- SQL server 中的三个重要对象
- OleDbCommand更新数据的一些问题
- 如何才能学好C#(转载)
- ASP.NET中使用Server.Transfer()方法在页间传值
- ASP.NET Tips1---合并多个字段值
- 2004.7.15 [C#学习记录]使用C#编写一个自定义控件(高
- Implementing the Singleton Pattern in C#
- 使用OleDbCommand对象更新SQL Server中的二进制文件
- 用C#轻松地在DOTNET中实现缩略图
- csdn空间改版啦~
- IDesign C#编程规范(二)
- BBC非常佩服中国网民的绝招
- 用WinDbg探索CLR世界 [4] 方法的调用机制
- 电子商务的安全性威胁来自如下几方面
- CLR 调试接口的架构与应用 [2] 调试框架
- 用VB编写一个屏幕颜色拾取器