如何删除错误:必须声明标量变量 [英] how to remove error : must declare the scalar variable
本文介绍了如何删除错误:必须声明标量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我无法删除此错误,我也不知道为什么我会遇到这些错误。
我的代码块是:
dt.OpenCon();
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue(@ DAY,DateTime.Today.Day);
cmd.Parameters.AddWithValue(@ Month,DateTime.Today.Month);
string str =SELECT MA_First_Name,MA_Email_Id FROM Master_Applicant WHERE DATEPART(DAY,MA_DOB)= @DAY AND DATEPART(MONTH,MA_DOB)= @Month;
DataSet ds = dt.ExecuteDataSet(str);
我想比较Day&表中出生日期的月份。
如何删除此错误。
解决方案
< pre lang =c#> dt.OpenCon();
string str = SELECT MA_First_Name,MA_Email_Id来自Master_Applicant WHERE DATEPART(DAY,MA_DOB)= @DAY AND DATEPART(MONTH,MA_DOB)= @Month;
SqlCommand cmd = new SqlCommand(str);
cmd.Parameters.AddWithValue( @ DAY,DateTime.Today.Day);
cmd.Parameters.AddWithValue( @ Month,DateTime.Today.Month);
DataSet ds = dt.ExecuteDataSet(cmd);
我已经改变了添加参数和执行sql语句的顺序,首先用你的sql语句创建命令并为其添加参数。最后执行命令。
Hello all,
I am unable to remove this error also i don't know why i am facing these error.
My code block is:
dt.OpenCon(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@DAY", DateTime.Today.Day); cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month); string str = "SELECT MA_First_Name,MA_Email_Id FROM Master_Applicant WHERE DATEPART(DAY, MA_DOB) = @DAY AND DATEPART(MONTH, MA_DOB) = @Month"; DataSet ds = dt.ExecuteDataSet(str);
I want to compare Day & month with the Date of birth in table.
How can i remove this error.
解决方案
dt.OpenCon(); string str = "SELECT MA_First_Name,MA_Email_Id FROM Master_Applicant WHERE DATEPART(DAY, MA_DOB) = @DAY AND DATEPART(MONTH, MA_DOB) = @Month"; SqlCommand cmd = new SqlCommand(str); cmd.Parameters.AddWithValue("@DAY", DateTime.Today.Day); cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month); DataSet ds = dt.ExecuteDataSet(cmd);
I have change the sequence of adding parameters and executing sql statement, first create the command with your sql statement and add parameters to it. finally execute the command.
这篇关于如何删除错误:必须声明标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文