SQL 参数问题
SQL Parameters Issue
这似乎是我的参数名称的明显拼写错误,但我无法发现它。错误信息是:
Must declare the scalar variable "@SelectedDepartment"
代码:
using Microsoft.ApplicationBlocks.Data;
using (SqlConnection batConnection = new SqlConnection(batConstr))
{
string actualsQuery = "SELECT * FROM Budget WHERE CostCenter = @SelectedDepartment AND Year = @SelectedYear";
using (SqlCommand actualsCommand = new SqlCommand(actualsQuery, batConnection))
{
actualsCommand.CommandType = CommandType.Text;
actualsCommand.Parameters.AddWithValue("@SelectedDepartment", budgetsActuals.SelectedDepartment);
actualsCommand.Parameters.AddWithValue("@SelectedYear", Convert.ToInt32(budgetsActuals.SelectedYear));
batConnection.Open();
ds = SqlHelper.ExecuteDataset(batConnection, CommandType.Text, actualsQuery);
}
}
我已经调试了代码并且可以确认数据库连接良好并且模型具有填充参数的值。我错过了什么吗?
ADO.NET的细微差别是,如果参数的值为null
,则不添加参数(为了输入目的,至少).因此,添加时 - 您应该检查 null
并改用 DbNull.Value
。
LasseV.Karlsen 在评论中是正确的;参数未传递给 ExecuteDataset(...) 方法。这对我有用:
using (SqlConnection batConnection = new SqlConnection(batConstr))
{
string actualsQuery = "SELECT * FROM Budget WHERE CostCenter =
@SelectedDepartment AND Year = @SelectedYear";
using (SqlCommand actualsCommand = new SqlCommand(actualsQuery, batConnection))
{
SqlParameter[] parameters = new SqlParameter[2];
parameters[0] = new SqlParameter("@SelectedDepartment",
budgetsActuals.SelectedDepartment);
parameters[0].Value = budgetsActuals.SelectedDepartment;
parameters[1] = new SqlParameter("@SelectedYear",
budgetsActuals.SelectedYear);
parameters[1].Value = budgetsActuals.SelectedYear;
batConnection.Open();
ds = SqlHelper.ExecuteDataset(batConstr, CommandType.Text, actualsQuery,
parameters);
}
}
这似乎是我的参数名称的明显拼写错误,但我无法发现它。错误信息是:
Must declare the scalar variable "@SelectedDepartment"
代码:
using Microsoft.ApplicationBlocks.Data;
using (SqlConnection batConnection = new SqlConnection(batConstr))
{
string actualsQuery = "SELECT * FROM Budget WHERE CostCenter = @SelectedDepartment AND Year = @SelectedYear";
using (SqlCommand actualsCommand = new SqlCommand(actualsQuery, batConnection))
{
actualsCommand.CommandType = CommandType.Text;
actualsCommand.Parameters.AddWithValue("@SelectedDepartment", budgetsActuals.SelectedDepartment);
actualsCommand.Parameters.AddWithValue("@SelectedYear", Convert.ToInt32(budgetsActuals.SelectedYear));
batConnection.Open();
ds = SqlHelper.ExecuteDataset(batConnection, CommandType.Text, actualsQuery);
}
}
我已经调试了代码并且可以确认数据库连接良好并且模型具有填充参数的值。我错过了什么吗?
ADO.NET的细微差别是,如果参数的值为null
,则不添加参数(为了输入目的,至少).因此,添加时 - 您应该检查 null
并改用 DbNull.Value
。
LasseV.Karlsen 在评论中是正确的;参数未传递给 ExecuteDataset(...) 方法。这对我有用:
using (SqlConnection batConnection = new SqlConnection(batConstr))
{
string actualsQuery = "SELECT * FROM Budget WHERE CostCenter =
@SelectedDepartment AND Year = @SelectedYear";
using (SqlCommand actualsCommand = new SqlCommand(actualsQuery, batConnection))
{
SqlParameter[] parameters = new SqlParameter[2];
parameters[0] = new SqlParameter("@SelectedDepartment",
budgetsActuals.SelectedDepartment);
parameters[0].Value = budgetsActuals.SelectedDepartment;
parameters[1] = new SqlParameter("@SelectedYear",
budgetsActuals.SelectedYear);
parameters[1].Value = budgetsActuals.SelectedYear;
batConnection.Open();
ds = SqlHelper.ExecuteDataset(batConstr, CommandType.Text, actualsQuery,
parameters);
}
}