我正在用c#asp.net構建一個應用程序。我需要將一些數據插入數據庫。一切工作正常,但我得到插入圖像的問題。無法將參數值從字節[]轉換爲字符串
我的數據庫表:
訂購
OrderID int
description varchar(50)
numOfItems int
material varchar(50)
image varbinary(max)
我將數據插入到數據庫代碼
protected void btnAddItem_Click(object sender, EventArgs e)
{
string filepath = fileUpload.PostedFile.FileName;
string filename = Path.GetFileName(filepath);
string ext = Path.GetExtension(filename);
string contentType = String.Empty;
switch (ext)
{
case ".jpg":
contentType = "image/jpg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".pdf":
contentType = "application/pdf";
break;
}
if (contentType != String.Empty)
{
Stream fs = fileUpload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string kon = ConfigurationManager.ConnectionStrings["mk"].ConnectionString;
using (SqlConnection conn = new SqlConnection(kon))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Order(description, numOfItems, material, image"))
{
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@description", inputTextArea.Text);
cmd.Parameters.AddWithValue("@numOfItems", inputTextArea.Text);
cmd.Parameters.AddWithValue("@material", inputTextArea.Text);
cmd.Parameters.Add("@image", SqlDbType.VarChar).Value = bytes;
conn.Open();
cmd.ExecuteNonQuery();
Response.Write("Success!");
}
}
}
}
當我這樣做,我得到以下錯誤:無法將參數值從Byte []轉換爲字符串。
有什麼建議嗎?
更新 - 新的錯誤
附近有語法錯誤 '形象'。錯誤。有任何想法嗎?
http://stackoverflow.com/questions/5824620/what-sqldbtype-maps-to-varbinarymax –