【SqlServer】【问题收集】必须声明标量变量
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 问题解决
将参数传递进去即可
1 public DataTable GetEmployeeToTable(string EmployeeName) 2 { 3 StringBuilder strSql = new StringBuilder(); 4 strSql.Append("SELECT EmployeeID,EmployeeName,EmployeeMajor, EmployeeDepartment,EmployeeTel,EmployeeEmail, EmployeeJiGuan,EmployeeAddress,EmployeePosition,EmployeeBirthday "); 5 strSql.Append(" WHERE EmployeeName=@EmployeeName "); 6 SqlParameter[] parameters = { 7 new SqlParameter("@EmployeeName",SqlDbType.VarChar,50) 8 }; 9 parameters[0].Value = EmployeeName; 10 return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0]; 11 }
3 版权
- 感谢您的阅读,若有不足之处,欢迎指教,共同学习、共同进步。
- 博主网址:http://www.cnblogs.com/wangjiming/。
- 极少部分文章利用读书、参考、引用、抄袭、复制和粘贴等多种方式整合而成的,大部分为原创。
- 如您喜欢,麻烦推荐一下;如您有新想法,欢迎提出,邮箱:2016177728@qq.com。
- 可以转载该博客,但必须著名博客来源。