zoukankan      html  css  js  c++  java
  • 从SQL Server中读写大数据列。

     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();
       
     }
  • 相关阅读:
    对文件下载的补充
    IBatisNet1.5学习配置篇
    IBatisnet Facility 的几种配置
    ERP术语 英文对照(部分)(参考)
    使用IBatisNet + Castle 开发DotNet软件
    JS屏蔽浏览器右键菜单
    恢复误删数据(SQL Server 2000)--Log Explorer
    IBatisNet1.5 映射文件Parameter Maps and Inline Parameters
    深圳电话订票基本步骤及所有的取票点地址电话
    DataFormatString格式化字符串
  • 原文地址:https://www.cnblogs.com/wxx/p/188969.html
Copyright © 2011-2022 走看看