简体   繁体   English

从C#对SQL Server执行插入查询时发生异常

[英]Exception while excecuting insert query to SQL Server from c#

My application was working well but customer wanted me to add some features to it. 我的应用程序运行良好,但客户希望我为其添加一些功能。 Now when I run the app any thing is working but by clicking a button that executes an insert query an exception occurs with this message: 现在,当我运行该应用程序时,一切正常,但是通过单击执行插入查询的按钮,将出现以下异常消息:

The parameterized query '(@ID int,@Subj nvarchar(50),@Pic varbinary(8000),@LetDate date,@' expects the parameter '@Pic', which was not supplied.

what are those silly parentheses? 那些傻括号​​是什么? the code of this part of program: 程序这部分的代码:

conn.Open();
string sqlcmd = "Insert into Pictures (ID, Subj, Pic, LetDate, LetTitle) Values (@ID, @Subj, @Pic, @LetDate, @LetTitle)";
insertCommand = new SqlCommand(sqlcmd, conn);
// For image data, we save the bytes into the database. We save the image to the JPG format bytes. 
insertCommand.Parameters.Add("ID", SqlDbType.Int).Value = (++lastID);
insertCommand.Parameters.Add("Subj", SqlDbType.NVarChar, 50).Value = textBox1.Text;
insertCommand.Parameters.Add("Pic", SqlDbType.VarBinary).Value = dynamicDotNetTwain1.SaveImageToBytes(lastIndex, Dynamsoft.DotNet.TWAIN.Enums.DWTImageFileFormat.WEBTW_JPG);
insertCommand.Parameters.Add("LetDate", SqlDbType.Date).Value = dateTimeSelector1.Value.Value.Date;
insertCommand.Parameters.Add("LetTitle", SqlDbType.NText).Value = titleTextBox1.Text;
index++;
int queryResult = insertCommand.ExecuteNonQuery();
if (queryResult == 1)
    MessageBox.Show("تصویر با موفقیت در پایگاه داده ذخیره شد", "پیغام", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();

This is an error occuring elsewhere in your program, and is unrelated to your stored procedure execution code. 这是程序中其他地方发生的错误,与存储过程执行代码无关。

The SQL error message "expects the parameter '@VariableName', which was not supplied." SQL错误消息“期望参数'@VariableName',未提供”。 usually indicates that you are setting a parameter value to null. 通常表示您将参数值设置为null。 If you actually intend to pass NULL to the query, you have to use the DBNull.Value value. 如果您实际上打算将NULL传递给查询,则必须使用DBNull.Value值。 However, it looks like you are trying to pass an actual value. 但是,看起来您正在尝试传递实际值。 Thus, the source of your issue is this line: 因此,问题的根源是以下行:

insertCommand.Parameters.Add("Pic", SqlDbType.VarBinary).Value =  dynamicDotNetTwain1.SaveImageToBytes(lastIndex, Dynamsoft.DotNet.TWAIN.Enums.DWTImageFileFormat.WEBTW_JPG);

If you can figure out why the dynamicDotNetTwain1.SaveImageToBytes() function is returning null and get that function working as expected, then you're stored procedure will work as expected. 如果您能弄清楚为什么dynamicDotNetTwain1.SaveImageToBytes()函数返回null并使该函数按预期工作,那么存储过程将按预期工作。

I think you have forgot to put @ infront of the variable names... 我认为您忘了在变量名前面加上@ ...

insertCommand.Parameters.Add("@ID", SqlDbType.Int).Value = (++lastID);
insertCommand.Parameters.Add("@Subj", SqlDbType.NVarChar, 50).Value = textBox1.Text;
insertCommand.Parameters.Add("@Pic", SqlDbType.VarBinary).Value = dynamicDotNetTwain1.SaveImageToBytes(lastIndex, Dynamsoft.DotNet.TWAIN.Enums.DWTImageFileFormat.WEBTW_JPG);
insertCommand.Parameters.Add("@LetDate", SqlDbType.Date).Value = dateTimeSelector1.Value.Value.Date;
insertCommand.Parameters.Add("@LetTitle", SqlDbType.NText).Value = titleTextBox1.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM