winform程序调试中出现异常
public static void Checkde(User NewUser){
SqlDataReader rdr;
string sql = @"SELECT UserName,Password from T_User WHERE UserName=@UserName and Password=@Password";
SqlParameter[] parameters =new SqlParameter[]{
new SqlParameter ("@UserName",SqlDbType.VarChar,50 ),
new SqlParameter ("@Password",SqlDbType .VarChar ,50)
};
parameters[0].Value = NewUser.UserName;
parameters[1].Value = NewUser.Password;
rdr=SqlHelper.ExecuteReader(null,CommandType.Text ,sql,parameters);
while (rdr.Read())
{
NewUser.UserName = rdr.GetString(1);
NewUser.Password = rdr.GetString(2);
}
}
public static class SqlHelper
{
public static string ConnectionString=@"Server=.;Database=MyDB;Integrated Security=True";
public static SqlDataReader ExecuteReader(SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
return cmd.ExecuteReader( ) ;
}
}
}
在连数据库验证后,在return cmd.ExecuteReader( )这出现异常未处理,错误显示:参数化查询 '(@UserName varchar(50),@Password varchar(50))SELECT UserName,Pas' 需要参数 @UserName,但未提供该参数。
该怎么办? --------------------编程问答-------------------- 你把NEW 出的那两个参数添加进去了吗? --------------------编程问答--------------------
在哪里添加参数啊? --------------------编程问答-------------------- 按理说你提供的两个参数应该要说明给谁用是吧。我觉得就是欠了一句代码你应该把参数加给那个:
cmd.Parameters.Add(“这里是参数名”);
试试看。
补充:.NET技术 , C#