如题,在数据库中以二进制方式保存照片,但现在就是想批量导出所需要的图片并自动保存成文件,用程序如何实现这样的功能,请高手指教!

解决方案 »

  1.   

    改改连接字符串,改改sql语句,传入你要保存的路径就好了
    private void GetAndWriteData(string path)
    {
    try
    {
    Byte[] blob = null;
    FileStream fs = null;
    // string strCnn = "server=(local);Initial Catalog=Master;UID=pl;PWD=";
    string strCnn = this.ConnectString;
    // string strSql = "select Data from UserControls where id = '"+this.FileID+"'";
    string strSql = "select "+this.DataFieldName+" from "+this.TableName+" where "+this.IDFieldName+" = '"+this.FileID+"'";

    SqlConnection Cnn = new SqlConnection(strCnn);
    SqlCommand Cmd = new SqlCommand(strSql,Cnn);
    Cnn.Open();
    SqlDataReader sReader = Cmd.ExecuteReader();
    sReader.Read();
    blob = new Byte[(sReader.GetBytes(0, 0, null, 0, int.MaxValue))];
    sReader.GetBytes(0, 0, blob, 0, blob.Length);
    sReader.Close();
    Cnn.Close();
    fs = new FileStream(path, FileMode.Create, FileAccess.Write);  fs.Write(blob, 0, blob.Length);
    fs.Close();

    }
    catch(SqlException ex)
    {
    MessageBox.Show("Sql Exception: " + ex.Message);
    }
    catch(Exception ex)
    {
    MessageBox.Show("Exception: " + ex.Message);
    }
    }