哦!谢谢大家!问题已经解决了! string str="insert into userInfo(username,password,role) values (''"+txtusername+"'','''"+txtpassword+"','0')"; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ToString());
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;namespace blog_garden { public partial class register : System.Web.UI.Page { SqlConnection myConn; protected void Page_Load(object sender, EventArgs e) { myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["blog"].ConnectionString.ToString()); } protected void TextBox1_TextChanged(object sender, EventArgs e) { myConn.Open(); string sql_string = "select name from blog_user where name='" + TextBox1.Text.Trim() + "'"; SqlDataAdapter myAdp = new SqlDataAdapter(sql_string, myConn); DataSet myds = new DataSet(); myAdp.Fill(myds, "用户表"); myConn.Close(); if (myds.Tables[0].Rows.Count == 0) Response.Write("<script language='javascript'>alert('用户名已存在')</script>"); } protected void Button1_Click(object sender, EventArgs e)//清除 { TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = ""; TextBox5.Text = ""; Label1.Text = ""; } protected void Button2_Click(object sender, EventArgs e)//新建用户 { if (RegularExpressionValidator1.IsValid == true && CompareValidator1.IsValid == true && TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "" && TextBox5.Text != "") { myConn.Open(); string sql_string = "select name from blog_user where name='" + TextBox1.Text.Trim() + "'"; SqlDataAdapter myAdp = new SqlDataAdapter(sql_string, myConn); DataSet myds = new DataSet(); myAdp.Fill(myds, "用户表"); myConn.Close(); if (myds.Tables[0].Rows.Count == 0) { sql_string = "insert into blog_user(name,password,nick_name,register_date,lastlogin_date,email)values('" + TextBox1.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox2.Text.Trim() + "',getdate(),getdate(),'" + TextBox6.Text.Trim() + "')"; SqlCommand cmd = new SqlCommand(sql_string, myConn); myConn.Open(); cmd.ExecuteNonQuery(); myConn.Close(); Session["UserName"] = TextBox2.Text.Trim(); this.Server.Transfer("user.aspx?user_name=" + TextBox1.Text.Trim()); } else { // Label1.Text = "博客名已存在,请选择其他博客名"; TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = ""; TextBox5.Text = ""; } } else { if (RegularExpressionValidator1.IsValid == false) Label1.Text = "电子邮件书写不正确"; if (CompareValidator1.IsValid == false) Label1.Text = "两次输入密码不一致"; if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "") Label1.Text = "请将上面项目填写完整"; } } } }
51aspx.com里很多
是这个把
建议楼主应该找一实例看看
网上应该有实例的
string str="insert into userInfo(username,password,role) values (''"+txtusername+"'','''"+txtpassword+"','0')";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ToString());
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;namespace blog_garden
{
public partial class register : System.Web.UI.Page
{
SqlConnection myConn;
protected void Page_Load(object sender, EventArgs e)
{
myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["blog"].ConnectionString.ToString());
} protected void TextBox1_TextChanged(object sender, EventArgs e)
{
myConn.Open();
string sql_string = "select name from blog_user where name='" + TextBox1.Text.Trim() + "'";
SqlDataAdapter myAdp = new SqlDataAdapter(sql_string, myConn);
DataSet myds = new DataSet();
myAdp.Fill(myds, "用户表");
myConn.Close();
if (myds.Tables[0].Rows.Count == 0)
Response.Write("<script language='javascript'>alert('用户名已存在')</script>");
}
protected void Button1_Click(object sender, EventArgs e)//清除
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
Label1.Text = "";
} protected void Button2_Click(object sender, EventArgs e)//新建用户
{
if (RegularExpressionValidator1.IsValid == true && CompareValidator1.IsValid == true && TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "" && TextBox5.Text != "")
{
myConn.Open();
string sql_string = "select name from blog_user where name='" + TextBox1.Text.Trim() + "'";
SqlDataAdapter myAdp = new SqlDataAdapter(sql_string, myConn);
DataSet myds = new DataSet();
myAdp.Fill(myds, "用户表");
myConn.Close();
if (myds.Tables[0].Rows.Count == 0)
{
sql_string = "insert into blog_user(name,password,nick_name,register_date,lastlogin_date,email)values('" + TextBox1.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox2.Text.Trim() + "',getdate(),getdate(),'" + TextBox6.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(sql_string, myConn);
myConn.Open();
cmd.ExecuteNonQuery();
myConn.Close(); Session["UserName"] = TextBox2.Text.Trim();
this.Server.Transfer("user.aspx?user_name=" + TextBox1.Text.Trim());
}
else
{
// Label1.Text = "博客名已存在,请选择其他博客名";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
}
else
{
if (RegularExpressionValidator1.IsValid == false)
Label1.Text = "电子邮件书写不正确";
if (CompareValidator1.IsValid == false)
Label1.Text = "两次输入密码不一致";
if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "")
Label1.Text = "请将上面项目填写完整";
}
}
}
}