[英]Retrieving an image from SQL Server
我編寫了這段代碼,但是遇到了一個異常: Parameter Not Valid.
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstr;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM tbl";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
byte[] barrImg = (byte[])dt.Rows[1]["image"];
MemoryStream mstream = new MemoryStream();
mstream.Write(barrImg, 0, barrImg.Length);
mstream.Seek(0, SeekOrigin.Begin);
img.Image = Image.FromStream(mstream);
mstream.Close();
例外是:
Parameter is not valid.
在Image.FromStream
代碼行中。
我檢查了datatable中分配給image字段的值。 那是System.Byte[]
。 我跟蹤了代碼。 每件事似乎都是正確的。 但這行不通。
我搜索了這個問題。 最好將另一個站點設置為mstream.Position = 0
。 但這不起作用。
我用這個代碼存儲了我的圖像,如果可能我保存錯了!
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstr;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string sql = "INSERT INTO tbl (name, family, image) VALUES ('name', 'family', '{0}')";
sql = string.Format(sql, Image.FromFile("test.jpg"));
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
conn.Close();
保存圖像的新代碼:
public byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
和:
private void cmdSave_Click(object sender, EventArgs e)
{
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
byte[] imageData = ReadFile("test.jpg");
SqlConnection CN = new SqlConnection(connstr);
string qry = "insert into tbl (name, family, image) VALUES ('name', 'family', '{0}')";
qry = string.Format(qry, imageData);
SqlCommand SqlCom = new SqlCommand(qry, CN);
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
}
嗯,那行不通。
您的“存儲”圖像的代碼實際上並沒有真正實現您認為的功能。
它將值 “ test.jpg”放入圖像字段。 那不是二進制圖像數據。 僅文件名 。
因此,當您將其撤回時,由於值“ test.jpg”不是圖像,因此Image.FromStream(mstream)
調用將刪除數據塊。ergo:該參數無效
這是實際將圖像放入數據庫中需要執行的操作示例:
http://www.codeproject.com/Articles/21208/Store-or-Save-images-in-SQL-Server
您關閉流,然后嘗試從流中提取圖像,因此錯誤“無法訪問關閉的流”。
嘗試交換最后兩行的順序:
img.Image = Image.FromStream(mstream);
mstream.Close();
using (MemoryStream mstream = new MemoryStream((byte[])dt.Rows[1]["image"]))
{
img.Image = Image.FromStream(mstream);
}
會簡化事情。 我懷疑您的問題是您需要在寫入之后和查找之前調用mStream.Flush()。
將圖像放入數據庫的一種方法是。
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
using(SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand comm = new SqlCommand(conn))
{
comm.CommandText = "INSERT INTO tbl (name, family, image) VALUES (@name,@family,@Content)";
comm.Parameters.Add(new SqlParameter("name",DbType.String,"Fred"));
comm.Parameters.Add(new SqlParameter("family",DbType.String,"Flintstone"));
using(FileStream fs = new FileStream("FredWilmaBamBamAndDino.jpg",FileMode.Open,FileAccess.Read))
{
comm.Parameters.Add(new SqlParameter("content",DbType.Image,fs));
}
cmd.ExecuteNonQuery();
}
}
注意,它將teh文件的內容放入數據庫中。 由於對於jpg而言,內容絕對不是字符串,因此請使用參數化查詢。 無論如何,您應該養成一個好習慣。 您還需要暫停使用,因為您的代碼正在各處泄漏資源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.