我正在用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 –