今天我問了太多問題,但我還有一個問題。我正在嘗試將圖像保存到我的數據庫中。我解決不了問題。當我嘗試添加圖像時,它表示參數字符串不能轉換爲BYTE []。其實我給字節[]作爲參數value.I試圖解決,但我找不到任何答案,也許你可以幫助我。這裏是我的代碼:字符串到字節數組轉換c#
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";
可能是問題就在這裏:_cmd.Parameters.Add( 「@ LIB」,SqlDbType.Binary).value的= 「NO」; _ –