数据库中有3个自动
ID,自动增长,pic/Image类型、存的图片信息、picSize/int类型、图片大小
怎样才可以保存在本地那、?

解决方案 »

  1.   

    //示例代码取pubs中pub_info表中的图像using System;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;class BLOBDemo
    {
    [STAThread]
    static void Main(string[] args)
    {
    SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=pubs");
    SqlCommand cmd = new SqlCommand("Select pub_id,logo FROM pub_info", cn); FileStream fs;
    BinaryWriter bw; //缓冲区大小
    const int bufferSize = 100;
    byte [] outByte = new byte[bufferSize];
    //GetBytes返回的字节数量
    long retval;
    //BLOB输出的起始位置
    long startIndex = 0; string pub_id = ""; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read())
    {
    pub_id = dr.GetString(0); fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
    bw = new BinaryWriter(fs); startIndex = 0;

    do
    {
    retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
    // Console.WriteLine(retval.ToString());
    bw.Write(outByte);
    bw.Flush();
    startIndex += bufferSize;
    }while(retval == bufferSize); bw.Write(outByte, 0, (int)retval - 1);
    bw.Flush(); bw.Close();
    fs.Close();
    } dr.Close();
    cn.Close();
    }
    }