日期:2014-05-19  浏览次数:20439 次

菜鸟问题:一个页面内不同方法中,变量名不能相同吗?
Button1_Click里调用了checkUsername方法,都定义了SqlCommand类型的cmd,会出错
我用cmd.Dispose();把他释放了也不行吗?
请大家帮我解释下


private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
cmd=MDB.DBExecSP( "sp_adduser ");

cmd.Parameters.Add( "@username ",SqlDbType.VarChar,50);
cmd.Parameters[ "@username "].Value=tb_username.Text.Trim();

cmd.Parameters.Add( "@pwd ",SqlDbType.VarChar,50);
cmd.Parameters[ "@pwd "].Value=tb_pwd.Text.Trim();

cmd.Parameters.Add( "@userclass ",SqlDbType.Int,4);
cmd.Parameters[ "@userclass "].Value=Convert.ToInt32(ddl_userclass.SelectedValue);

if(checkUsername(tb_username.Text.Trim()))
{
try
{
cmd.ExecuteNonQuery();
Response.Write(MDB.DBAlert( "添加成功! "));
tb_username.Text= " ";
tb_pwd.Text= " ";
}
catch
{
Response.Write(MDB.DBAlert( "添加失败! "));
}
}
else
{
Response.Write(MDB.DBAlert( "该用户名已经存在! "));
}
}

//检查该用户名是否存在
private   bool   checkUsername(string   username)
{
cmd=MDB.DBExecSP( "sp_checkusername ");
cmd.Parameters.Add( "@username ",SqlDbType.VarChar,50);
cmd.Parameters[ "@username "].Value=username;

cmd.Parameters.Add( "@countNum ",SqlDbType.Int,4);
cmd.Parameters[ "@countNum "].Direction=ParameterDirection.Output;

cmd.ExecuteNonQuery();

int   i=Convert.ToInt32(cmd.Parameters[ "@countNum "].Value);
cmd.Dispose();
if(i   <   1)
{
return   true;
}
else
{
return   false;
}
}


------解决方案--------------------
checkUsername里调用
cmd.connection.close()