当我通过提供用户名和密码登录时,我得到一个例外:
“参数化查询”(@username nvarchar(4000),@ password12 nvarchar(4000))选择我'期待 参数'@username',未提供。“
通过代码:
public DataSet showoption1(seter set)
{
string sql = "SELECT menue.menuename,roleid, [right].rightname,
role.rolename, employee.employeename, employee.password FROM [right] INNER JOIN
permission ON [right].rightid = permission.rightid INNER JOIN role ON
permission.roleid = role.roleid INNER JOIN emp_men INNER JOIN employee ON
emp_men.employeeid = employee.employeeid ON role.roleid = employee.roleid
INNER JOIN menue ON emp_men.menueid = menue.menueid
where employee.employeenamee=@username
AND employee.password=@password12";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username",set.Username);
cmd.Parameters.AddWithValue("@password12",set.Password);
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adptr.Fill(ds);
int id= Convert.ToInt32( ds.Tables[2].Rows[0]["rooleid"]);
st.Username= Convert.ToString(ds.Tables[2].Rows[id]["roolename"]);
adptr.Dispose();
return ds;
}
答案 0 :(得分:2)
由于数据库参数的工作原理很奇怪,如果set.Username
为null
,则会发生这种情况。因此:检查set.Username
是否为null
,并且在这种情况下可能会引发异常(因为null
用户名听起来像是一个错误)。
if(string.IsNullOrWhiteSpace(set.Username))
throw new InvalidOperationException("No username was provided");
在更一般的情况下,如果您执行想要传递null
,则必须使用DBNull.Value
,例如:
object username = set.Username;
if(username == null) username = DBNull.Value;
cmd.Parameters.AddWithValue("@username", username);
答案 1 :(得分:0)
你有两个INNER JOIN背靠背没有相应的ON ...导致解析错误???
SELECT
menue.menuename,
roleid,
[right].rightname,
role.rolename,
employee.employeename,
employee.password
FROM
[right]
INNER JOIN permission
ON [right].rightid = permission.rightid
INNER JOIN role
ON permission.roleid = role.roleid
INNER JOIN employee
ON role.roleid = employee.roleid
INNER JOIN emp_men
ON emp_men.employeeid = employee.employeeid
INNER JOIN menue
ON emp_men.menueid = menue.menueid
where
employee.employeenamee = @username
AND employee.password=@password12";
答案 2 :(得分:0)
我认为最可能的解释是set.Username
为空。
逐步调试并检查值。