我的意图时不能有相同的用户名和邮箱。
但我感觉写的蛮复杂的。。有哪里可以简化一下吗?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class UserRegisterValidate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("用户名为:" + TextBox1.Text + "<br/>");
Response.Write("密码为:" + TextBox2.Text + "<br/>");
Response.Write("年龄为:" + TextBox4.Text + "<br/>");
Response.Write("邮箱为:" + TextBox5.Text + "<br/>"); {
SqlConnection sqlcon = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlcon.Open();
SqlCommand Cmdc = new SqlCommand("select count(*) from id where name='" + TextBox1.Text + "'", sqlcon);
int count = Convert.ToInt32(Cmdc.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('该用户名已经存在');location='javascript:history.go(-1)'</script>");
}
}
{
SqlConnection sqlcon = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlcon.Open();
SqlCommand Cmdc = new SqlCommand("select count(*) from id where email='" + TextBox5.Text + "'", sqlcon); int count = Convert.ToInt32(Cmdc.ExecuteScalar()); if (count > 0)
{
Response.Write("<script>alert('该邮箱已经存在');location='javascript:history.go(-1)'</script>");
} else
{
SqlConnection sqlconn = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlconn.Open();
SqlCommand Cmd = new SqlCommand();
Cmd.Connection = sqlconn;
Cmd.CommandText = "Insert into id([name],[password],[age],[email]) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" +
TextBox5.Text + "')";
Cmd.ExecuteNonQuery();
sqlconn.Close();
Response.Redirect("~/index.htm");
}
}
}
}
但我感觉写的蛮复杂的。。有哪里可以简化一下吗?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class UserRegisterValidate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("用户名为:" + TextBox1.Text + "<br/>");
Response.Write("密码为:" + TextBox2.Text + "<br/>");
Response.Write("年龄为:" + TextBox4.Text + "<br/>");
Response.Write("邮箱为:" + TextBox5.Text + "<br/>"); {
SqlConnection sqlcon = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlcon.Open();
SqlCommand Cmdc = new SqlCommand("select count(*) from id where name='" + TextBox1.Text + "'", sqlcon);
int count = Convert.ToInt32(Cmdc.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('该用户名已经存在');location='javascript:history.go(-1)'</script>");
}
}
{
SqlConnection sqlcon = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlcon.Open();
SqlCommand Cmdc = new SqlCommand("select count(*) from id where email='" + TextBox5.Text + "'", sqlcon); int count = Convert.ToInt32(Cmdc.ExecuteScalar()); if (count > 0)
{
Response.Write("<script>alert('该邮箱已经存在');location='javascript:history.go(-1)'</script>");
} else
{
SqlConnection sqlconn = new SqlConnection("Data Source=(local);DataBase=user;User ID=sa;PWD=yangguo");
sqlconn.Open();
SqlCommand Cmd = new SqlCommand();
Cmd.Connection = sqlconn;
Cmd.CommandText = "Insert into id([name],[password],[age],[email]) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" +
TextBox5.Text + "')";
Cmd.ExecuteNonQuery();
sqlconn.Close();
Response.Redirect("~/index.htm");
}
}
}
}
解决方案 »
- 高分请人帮忙调试一下网站.着急死了.
- 网站api如何做安全保护,防止非法调用?
- |zyciis| 求正则表达式,或其他方法, 删除一段字符串中开头和结尾的空格和换行符
- 关于左侧菜单的隐藏 散分
- vs2005元素***不是已知元素,原因可能是网站中存在编译错误
- 能不能在Treeview的每个节点后面加上一个或多个ImageButton,以对这个节点进行一些操作
- 如果暂时停掉.net2.0改用.net1.1该怎样做?
- 请问一下从数据库中读取类目形成下拉式的网页菜单怎么做呢?
- 在xxx.aspx.cs文件里面如何实现向客户端写文件下载?
- 请问有没有人研究过ASPNETFORUM?
- 用table表如何数据绑定5条信息,并实现翻页功能?谢谢大家了!
- =======请CSDN换回原版======
代码的确是可以简化的,
比如上面的建立连接,你完全可以把它写成一个公共类,还有查询用户名是否重复,邮箱是否重复,写成一个公司用类就可以了。给你一些提示吧:
public int mydata()
{
//这里假设你的数据库连接conn和command已经存在了
int i=Convert.ToInt32(cmd.ExecuteScalar());
return i;
}
还有,像你那样直接向数据库中插入数据是不行的,最好用参数的形式
每次使用调用就可以了