0

当我通过提供用户名和密码登录时,出现异常:

“参数化查询 '(@username nvarchar(4000),@password12 nvarchar(4000))SELECT me' 需要参数 '@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;

   }
4

3 回答 3

2

由于 DB 参数的工作方式很奇怪,如果set.Usernamenull. 所以:检查是否set.Usernameis 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);
于 2012-06-15T11:51:32.133 回答
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";
于 2012-06-15T11:54:42.637 回答
0

我认为最可能的解释set.Username是空值。

使用调试器单步执行并检查值。

于 2012-06-15T11:57:00.453 回答