请问代码如何写才能判断用户名是否已存在??我的代码如下,请问如何进行下去??谢谢了!
string strSql="select * from 用户表 where 用户名='"+newUID+"'";
SqlCommand objCmd=new SqlCommand(strSql,objConn);
string strSql="select * from 用户表 where 用户名='"+newUID+"'";
SqlCommand objCmd=new SqlCommand(strSql,objConn);
{
if(this.IsValid)
{
string strName=this.txtAdmin.Text.ToString().Trim().Replace(">","''").Replace("<","''");
string strPwd=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPwd.Text.ToString().Trim(),"md5");
SqlConnection con=DB.createCon();
con.Open();
SqlCommand cmd=new SqlCommand("select top 1 * from adminInfo where adminName=@strName",con);
cmd.Parameters.Add(new SqlParameter("@strName",SqlDbType.VarChar,50));
cmd.Parameters["@strName"].Value=strName;
SqlDataReader sdr=cmd.ExecuteReader();
try
{
sdr.Read();
if(sdr.HasRows)
{
string adminName,adminPwd;
dminName=sdr["adminName"].ToString().Trim();
adminPwd=sdr["adminPwd"].ToString().Trim();
if(strPwd!=adminPwd)
{
Response.Write("<script language=javascript>alert('您输入的密码错误!请重新登陆!!');</script>");
}
else
{
if(Session["login"]==null)
{
Session["login"]=strName;
Response.Redirect("Admin.aspx");
}
}
}
else
{
// this.lblMessage.Visible=true;
// this.lblMessage.Text="对不起!登陆用户名为["+strName+"]的用户不存在!";
Response.Write("<script language=javascript>alert('对不起!登陆用户名为["+strName+"]的用户不存在!');</script>");
}
}
catch(Exception ex)
{
this.lblMessage.Visible=true;
this.lblMessage.Text=ex.Message;
if(ex.GetBaseException() is System.Web.HttpRequestValidationException)
Response.Write("<script language=javascript>alert('您的输入中有非法字符,请正确输入!');</script>");
用户名不存在
else
用户名存在
{
//如果有记录的话就。。
}