网页功能: 加入收藏 设为首页 网站搜索  
从SQL Server中读写大数据列
发表日期:2005-07-17作者:[转贴] 出处:  

 

/*
  Author:Wu Xiuxiang;
 Email:imessage@126.com

*/

public static void Main()
 
{
   
//写入大对象到SqlServer
   FileStream fs   = new FileStream("C:\\test.bmp",FileMode.OPen,FileAccess.Read);
   BinaryReader br 
= new BinaryReader(fs);

   SqlConnection conn 
= new SqlConnection("server=localhost;uid=sa;pwd=sa;database=northwind");

   
string cmdText = "UPDATE EMPLOYEES" +
         
"SET Photo=@image where EmployeeId=1";
   
   SqlCommand cmd 
= new SqlCommand(cmdText,conn);
   cmd.Parameters.Add(
"@image",SqlDbType.Image);

   cmd.Parameters[
"@image"].Value = br.ReadBytes((int)br.BaseStream.Length);

   conn.Open();
   
int i=cmd.ExecuteNoQuery();


   
//从SQL Server中读取大对象
   string cmdtext = "SELECT employeeid,photo" +
          
" from employees where employeeid = 1";
   
 
  SqlCommand cmd2 
= new SqlCommand(cmdtext,conn);

  FileStream rfs;
  BinaryWriter rbw;

  
long numread;
  
long startIndex;
  
int buffSize=4096;
  
byte[] buff = new byte[buffSize];

  conn.Open();
  SqlDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.SequentialAccess);
  
  
if(rdr.Read())
  
{
     
int empid = rdr.GetInt32(0);

     fs 
= new FileStream("c:\\mypic.bmp",FileMode.OpenOrCreate,FileAccess.Write);
     bw 
= new BinaryWrite(fs);
     
     startIndex
=0;

     numread 
= rdr.GetBytes(1,startIndex,buff,0,buffSize);

     
while(numread==buffSize)
     
{
         bw.Write(buff);
         bw.Flush();
         startIndex
+=buffSize;
         numread 
= rdr.GetBytes(1,startIndex,buff,buffSize);
     }

     bw.Write(buff);
     bw.Flush();
     
     bw.Close();
     fs.Close();  
  }

  rdr.Close();
  conn.Close();
   
 }

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 从SQL Server中读写大数据列
本类热点文章
  简单的多人聊天(C#.Socket)
  C#的office操作[word&Excel]
  在C#中调用VBScript、JavaScript等脚本..
  在C#中调用VBScript、JavaScript等脚本..
  C#程序编码规范
  C#程序编码规范
  使用c#获得cpu,硬盘的序列号
  在Linux上运行C#
  在Linux上运行C#
  弹出窗口杀手
  弹出窗口杀手
  算术表达式解析器的实现思路
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00421