private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) { string username=args.Value; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source="+HttpContext.Current.Server.MapPath("db.mdb")); conn.Open(); OleDbCommand cmd=new OleDbCommand("select Count(*) from GuestBook where Username='"+this.TextBox8.Text+"'",conn); int count=Convert.ToInt32(cmd.ExecuteScalar()); if(count>0) { args.IsValid=false; } else { args.IsValid=true; } conn.Close(); } 这里使用的是ACCESS数据库 转载于:https://www.cnblogs.com/icejd/archive/2006/04/01/364399.html