绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
【SqlServer】【问题收集】必须声明标量变量
2023-02-15 15:40:59

1   问题概述

 在DAL层,通过标量给变量赋值时,出现如下异常

 


我们来看看在数据访问层的SQL语句:

//根据EmployeeName条件获取数据
public DataTable GetEmployeeToTable(string EmployeeName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT EmployeeID,EmployeeName,EmployeeMajor, EmployeeDepartment,EmployeeTel,EmployeeEmail, EmployeeJiGuan,EmployeeAddress,EmployeePosition,EmployeeBirthday ");
strSql.Append(" WHERE EmployeeName=@EmployeeName ");
SqlParameter[] parameters = {
new SqlParameter("@EmployeeName",SqlDbType.VarChar,50)
};
parameters[0].Value = EmployeeName;

return DbHelperSQL.GetDataTable(strSql.ToString());
}

2   问题分析与解决

2.1 问题分析

变量参数未传递进去

2.2  问题解决

将参数传递进去即可

public DataTable GetEmployeeToTable(string EmployeeName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT EmployeeID,EmployeeName,EmployeeMajor, EmployeeDepartment,EmployeeTel,EmployeeEmail, EmployeeJiGuan,EmployeeAddress,EmployeePosition,EmployeeBirthday ");
strSql.Append(" WHERE EmployeeName=@EmployeeName ");
SqlParameter[] parameters = {
new SqlParameter("@EmployeeName",SqlDbType.VarChar,50)
};
parameters[0].Value = EmployeeName;
return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
}

3   版权

本文来源https://www.cnblogs.com/wangjiming/p/7615618.html

分享好友

分享这个小栈给你的朋友们,一起进步吧。

Microsoft SQL Server
创建时间:2022-03-30 11:29:11
Microsoft SQL Server
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • itt0918
    专家
戳我,来吐槽~