如何从sql数据库中获取图片?如何扩大二进制列的大小?

时间:2016-08-25 12:38:08

标签: c# sql sql-server winforms

我有一个名字& picutre使用此方法保存在数据库中:

Image img = Image.FromFile(imgLoc);
            MemoryStream tmpStream = new MemoryStream();
            img.Save(tmpStream, System.Drawing.Imaging.ImageFormat.Png);
            tmpStream.Seek(0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[4000];
            tmpStream.Read(imgBytes, 0, 4000);

            string sqlquery = ("INSERT INTO Firma (Name, Logo)" +
                   "Values(@name, @logo)");
            SqlCeCommand cmd = new SqlCeCommand(sqlquery, cn);
            cmd.Parameters.AddWithValue("@name", tbName.Text);
            cmd.Parameters.AddWithValue("@logo", imgBytes);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Erfolgreich hinzugefügt!");
            cn.Close();
            this.Close();

现在我想将图片放回并显示在图片框中。 我的代码不起作用。

cn.Open();
        comm = "SELECT Logo From Firma WHERE FirmenNr LIKE @Firma ";
        cmd = new SqlCeCommand(comm, cn);
        cmd.Parameters.Add("@Firma", SqlDbType.NVarChar, 100).Value = FirmenNr.ToString();

        SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(cmd);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);

        if (dataSet.Tables[0].Rows.Count == 1)
        {
            Byte[] data = new Byte[0];
            data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);
            MemoryStream mem = new MemoryStream(data);
            pictureBox1.Image = Image.FromStream(mem);
        } 

        cn.Close();

图片以二进制形式保护,长度为4000。 编辑:我怎么能把它做大? visual studio不允许我将其设置为4030以上的值。

2 个答案:

答案 0 :(得分:0)

你在这里犯了错误:

data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);

您正在访问pic列,但查询Logo

答案 1 :(得分:0)

云端硬盘 将文件放入数据库的方法:

 public static void databaseFilePut(string imgLoc) {
    byte[] file;
    using (var stream = new FileStream(imgLoc, FileMode.Open, FileAccess.Read)) {
        using (var reader = new BinaryReader(stream)) {
            file = reader.ReadBytes((int) stream.Length);       
        }          
    }
    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
    using (var sqlWrite = new SqlCommand("INSERT INTO Firma(Name, Logo)Values(@File)", varConnection)) {
        sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
        sqlWrite.ExecuteNonQuery();
    }
}

此方法是从数据库和显示在图片工具获取文件

  public static void databaseFileRead(string varID, string varPathToNewLocation) {
     MemoryStream memoryStream = new MemoryStream();
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
using (var sqlQuery = new SqlCommand(@"SELECT [Logo] FROM [dbo].[Firma] WHERE [RaportID] = @varID", varConnection)) {
    sqlQuery.Parameters.AddWithValue("@varID", varID);
    using (var sqlQueryResult = sqlQuery.ExecuteReader())
        if (sqlQueryResult != null) {
            sqlQueryResult.Read();
            var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
            sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
            //using (var fs = new MemoryStream(memoryStream, FileMode.Create, FileAccess.Write)) {
            memoryStream.Write(blob, 0, blob.Length);
            //}
        }
}

   pictureBox1.Image = Image.FromStream(memoryStream);

}