我今天问了太多问题,但我还有一个问题。我正在尝试将图像保存到我的数据库中。我无法解决问题。当我尝试添加图像时,它说参数字符串无法转换为 BYTE []。实际上我将字节 [] 作为参数值。我试图解决,但我找不到任何答案,也许你可以帮助我。这是我的代码:
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string strQuery = "INSERT INTO Books(Book_Name, Author_Name, Image,In_Lib) VALUES (@BN, @AN, @IM,@LIB)";
SqlCommand cmd = new SqlCommand(strQuery);
string val1 = "" + TextBox1.Text;
string val2 = "" + TextBox2.Text;
cmd.Parameters.Add("@BN", SqlDbType.VarChar).Value = val1;
cmd.Parameters.Add("@AN", SqlDbType.VarChar).Value= val2;
cmd.Parameters.Add("@IM", SqlDbType.Binary).Value = bytes;
cmd.Parameters.Add("@LIB", SqlDbType.Binary).Value = "NO";
InsertUpdateData(cmd);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Uploaded Successfully";