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