验证用户名是否被占用

 1 建立一个DB类用于和数据库的连接

 public static SqlConnection createConnection()
    {
        SqlConnection mystring = new SqlConnection("Initial Catalog =userReg; Data Source =5FAF81B03F644F7//FGFDS ;UID = sa;pwd = 222222");
        return mystring;
    }

2  用户控件事件

  }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
       // string name = args.Value;
        SqlConnection mystring = DB.createConnection();
        SqlCommand myCommand = new SqlCommand("select count(*) from wuqiao where id='" + args.Value + "'", mystring);
        mystring.Open();
        int count = Convert.ToInt32(myCommand.ExecuteScalar());
        if (count > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
        mystring.Close();
    }

你可能感兴趣的:(ASP.NET学习笔记)