我的update()
方法遇到了一些問題。這個想法是,用戶提供配方名稱,成分,說明,然後使用文件流選擇一個圖像。將數據插入表中的語法不正確
一旦用戶點擊「添加配方」,這將調用Update方法,但就當前情況來看,我越來越被提的文本框的內容的錯誤:
這裏是update()方法的代碼:
private void updatedata()
{
// filesteam object to read the image
// full length of image to a byte array
try
{
// try to see if the image has a valid path
if (imagename != "")
{
FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
// a byte array to read the image
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
//open the database using odp.net and insert the lines
string connstr = @"Server=mypcname\SQLEXPRESS;Database=RecipeOrganiser;Trusted_Connection=True";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string query;
query = "insert into Recipes(RecipeName,RecipeImage,RecipeIngredients,RecipeInstructions) values (" + textBox1.Text + "," + " @pic" + "," + textBox2.Text + "," + textBox3.Text + ")";
SqlParameter picparameter = new SqlParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pic";
picparameter.Value = picbyte;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(picparameter);
cmd.ExecuteNonQuery();
MessageBox.Show("Image successfully saved");
cmd.Dispose();
conn.Close();
conn.Dispose();
Connection();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
任何人都可以看到我已經錯的INSERT INTO食譜查詢或建議的替代方法的代碼的一部分?
請使用參數化查詢.. – ffffff01
也將所有RecipeIngredients填充到單個列中不是標準化設計,您應該使用'using'塊。 –
你沒有引用你的文本值。在每個值之前和之後放一個單引號(')。 –