[英]Must declare the scalar variable
我收到此错误...
必须声明标量变量“ @Photo”
我的插入代码看起来像这样...
if (image1.Source != null)
{
FileStream fs = new FileStream("@Photo", FileMode.Open, FileAccess.Read);
byte[] imgByteArr = new byte[fs.Length];
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
cmd.Parameters.Add(new SqlParameter("Photo", imgByteArr));
}
else
{
nonqueryCommand.Parameters.AddWithValue("@Photo", DBNull.Value);
}
SQL Server:列名称Photo
和数据类型image
我从这个网站尝试了一个例子。 我对其进行了一点修改,因为该网站使用了一个单击事件处理程序,而我想使用两个单击事件处理程序。 一个用于浏览按钮,另一个用于插入数据。 以下代码在插入句柄中
FileStream fs = new FileStream("@Photo", FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
nonqueryCommand.Parameters.AddWithValue("@Photo", data);
上面的代码给我一个错误...
找不到文件'C:.... \\ bin \\ Debug \\ @Photo'。
我认为这给了我错误,因为我没有使用编码相同方法的打开对话框。
预先感谢任何人可以帮助我。
更新1a
这将花费我很少的时间,因此我将离开。 我会从帮助过的人那里得到建议。 谢谢
您缺少带有参数的@
cmd.Parameters.Add(new SqlParameter("@Photo", imgByteArr));
////^^^^ here
nonqueryCommand.Parameters.AddWithValue("@Photo", DBNull.Value);
改成
nonqueryCommand.Parameters.AddWithValue("Photo", DBNull.Value);
还有这个
new FileStream("@Photo", FileMode.Open, FileAccess.Read);
显然是错误的。 什么是@Photo? 您不能将SQL列直接绑定到C#代码。 您需要从数据库中检索值,将其存储在某个变量中,然后才能使用它。 否则,此代码将仅尝试查找名为@Photo的文件,这将导致异常,如您所见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.