【发布时间】:2013-10-20 15:32:49
【问题描述】:
我的update() 方法遇到了一些问题。这个想法是用户提供食谱名称、成分、说明,然后使用 Filestream 选择图像。
一旦用户点击“添加食谱”,这将调用更新方法,但就目前情况而言,我收到一个错误,提示文本框的内容:
这里是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);
}
}
谁能看到我在插入到食谱查询时出错的地方或建议这部分代码的替代方法?
【问题讨论】:
-
请使用参数化查询..
-
另外,将所有 RecipeIngredients 填充到单个列中也不是标准化设计,您应该使用
using块。 -
你没有引用你的文本值。在每个值之前和之后放置一个单引号(')。
标签: c# .net sql-server-2008