日期:2014-05-17  浏览次数:20558 次

ASP.NET insert插入语句问题
各位大虾。
  我写了一段新用户注册代码,代码如下,不报错也不能插入数据库,希望给我大虾帮帮这只菜鸟

 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 zhucexinyonghu : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
   
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  string username =this.TextBox1.Text;
  string password = this.TextBox2.Text;
  string name = this.TextBox4.Text;
  string question = this.TextBox5.Text;
  string answer = this.TextBox6.Text;
  string phonenumber = this.TextBox7.Text;
   
  SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString11"].ConnectionString);
  cn.Open();
  string sqlsel = "select * from Users where UserName='" + TextBox1.Text + "'";
  SqlCommand cmd = new SqlCommand(sqlsel ,cn);
  if (TextBox1.Text==""||TextBox2.Text==""||TextBox4.Text==""||TextBox5.Text==""||TextBox6.Text=="")
  {
  RegisterStartupScript("", "<script>alert('打*的文本框为必填')</script>");
  }
  else if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  {
  RegisterStartupScript("", "<script>alert('对不起该用户名已经被注册过')</script>");
  }
  else if(TextBox3.Text!=TextBox2.Text)
  {
  RegisterStartupScript("", "<script>alert('你两次输入的密码不相同')</script>");
  }
  else
  {
  SqlConnection cn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString11"].ConnectionString);
  cn1.Open();
  string sqlsel1 = "insert into [Users]([UserName],[Password],[Name],[Question],[Answer],[PhoneNumber]) values('" + username+ "','" +password +"','" + name + "','" + question + "','" + answer + "''" + phonenumber + "')";
   
  SqlCommand cmd1 = new SqlCommand(sqlsel1, cn1);
  try
  {
  RegisterStartupScript("", "<script>alert('注册成功')</script>");
  }
  catch
  {
  RegisterStartupScript("", "<script>alert('注册失败')</script>");
  }
   

  }

  }
  
}


------解决方案--------------------
string sqlsel1 = "insert into [Users]([UserName],[Password],[Name],[Question],[Answer],[PhoneNumber]) values('" + username+ "','" +password +"','" + name + "','" + question + "','" + answer + "','" + phonenumber + "')";
------解决方案--------------------
string sqlsel = "select count(*) from Users where UserName='" + TextBox1.Text + "'";

还有就是1楼所说,另外

try
{
cmd1.ExecuteNonQuery()