这是我的用户注册的代码,在userreg.aspx.cs 中,现在插入记录已经正常了,但我该怎样检测输入的用户名:username 和 数据表 theuser 中字段up_user 中的任何一条不同呢。(唯一性检查)。请高手们帮我插入一下那些代码。protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
String sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open(); command.ExecuteNonQuery();
conn.Close();
Response.Write("OK");
}
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
String sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open(); command.ExecuteNonQuery();
conn.Close();
Response.Write("OK");
}
解决方案 »
- asp.net 能不能实现局域网发邮件
- Image 控件显示图片
- 一个叫ksproxy.ax进程,占用的CPU过高
- GridView里外键的值
- 一个关于gridview显示的简单问题,在线等!!!!!!!!
- 在线等,正则匹配 Server.UrlEncode处理过的字符串
- DELETE 语句与 COLUMN REFERENCE 约束 'FK__titleauth__au_id__0519C6AF' 冲突
- 如何在asp.net后台循环控制前台页面显示几个样式相同的div
- 好几天了,简单的SMTP发送邮件还是没有解决,哪位大虾指点下啊,先谢了
- 有人熟悉.net、CMS(content management server)、SPS(sharepoint portal server)吗?
- 怎样让ASP.net进程运行在其它用户下,而不是在ASPNET用户下?
- 关于double类型数字的问题!
判断这个值 是不是大于等于1
if exists(select [username] from [table_name] where [username]='user_name')
这个效率比count高
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
String sql = "if exists(select username from tablename where username ='"+user_name+" ')";
sql += string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open(); command.ExecuteNonQuery();
conn.Close();
Response.Write("OK");
}
{
Response.Write("用户名已存在");
Response.Redirect("userreg.aspx");
}
else
{
sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open(); command.ExecuteNonQuery();
conn.Close();
Response.Write("OK");
}
}
strsql="select count(*) from table1 where username='"+username+"'"
SqlCommand cmd=new SqlCommand(Session["chartsql"].ToString(),con);
cmd.CommandType=CommandType.Text;
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds,"t");
if(ds.Tables["t"].Rows.Count>0)
{
Response.Write("该用户已存在");
}
这样就OK了!!
Dim usern As String = Trim(txt1.Text)
Dim mima As String = Trim(txt2.Text)
Dim sconstr As String = _
System.Configuration.ConfigurationManager.AppSettings("scon")
Dim con As SqlClient.SqlConnection = _
New SqlClient.SqlConnection(sconstr)
Dim sql As String = " insert into test(usern,mima) values(@usern,@mima) "
Dim cmd As SqlClient.SqlCommand = _
New SqlClient.SqlCommand(sql, con)
con.Open()
Try
cmd.Parameters.Add(New SqlClient.SqlParameter("@usern", _
SqlDbType.VarChar, 50)).Value = usern
cmd.Parameters.Add(New SqlClient.SqlParameter("@mima", SqlDbType.VarChar, _
50)).Value = mima
cmd.ExecuteReader()
lab2.Text = "注册成功"
Catch ex As Exception
lab2.Text = "帐号存在重复"
End Try
con.Close()
/// 检验是否存在数据
/// </summary>
/// <returns></returns>
public bool ExistDate(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataReader Dr ;
Dr = CreateCmd(SQL,Conn).ExecuteReader();
if (Dr.Read())
{
Dispose(Conn);
return true;
}
else
{
Dispose(Conn);
return false;
}
}
同志们提供的代码我不知道怎么用进去。就在这上面改吧!谢谢了!
或者用IF ……ELSE ……行吗? protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
try
{
String sql = "if exists(select up_user from theuser where up_user ='" + username + " ')";
Response.Write("用户名被占用");
}
catch
{
String sql = string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn);
conn.Open();
command.ExecuteNonQuery();
Response.Write("注册成功");
}
conn.Close();
}
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
try
{
String sql = "if exists(select up_user from theuser where up_user ='" + username + " ') begin select '用户名被占用' end else
begin insert into theuser(up_user,up_pass,up_email)
values ('"+u_username+"', '"+pass+"', '"+textemail+"')
select '注册成功'
end
"; System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn);
conn.Open();
command.ExecuteNonQuery(); }
catch
{
Response.Write("注册失败");
}
conn.Close();
}
{
SqlConnection conn = new SqlConnection();
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["upupSQLConnectionString"];
String connString = s.ConnectionString;
conn.ConnectionString = s.ConnectionString;
String u_username = this.username.Text.ToString();
String pass = this.password.Text.ToString();
String textemail = this.email.Text.ToString();
String sql = "if exists(select username from tablename where username ='"+user_name+" ')";
sql += string.Format("insert into theuser(up_user,up_pass,up_email) values('{0}','{1}','{2}')", u_username, pass, textemail);
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql,conn);
conn.Open(); command.ExecuteNonQuery();
conn.Close();
Response.Write("OK");
}