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

注册时没有值传到数据库
public partial class web_register : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  public void btn2_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = "Data Source=.;Initial Catalog=yishilala;Integrated Security=True";
  //SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["yishilalaConnectionString"].ConnectionString);
  conn.Open();
  string sql = "insert into [User](UserName,Password,Password2,Sex,Email,Phone,PostCode,Address)value(@UserName,@Psaaword,@Password2,@Sex,@Email,@Phone,@PostCode,@Address)";
  SqlCommand cmd = new SqlCommand(sql,conn);
  cmd.Parameters.Add("@UserName",SqlDbType.VarChar,50);
  cmd.Parameters["@UserName"].Value = this.UserName;

  cmd.Parameters.Add("@Password",SqlDbType.VarChar,50);
  cmd.Parameters["@Password"].Value = this.pad;

  cmd.Parameters.Add("@Password2",SqlDbType.VarChar,50);
  cmd.Parameters["@Password2"].Value = this.pad2;

  cmd.Parameters.Add("@Email",SqlDbType.VarChar,50);
  cmd.Parameters["@Email"].Value = this.email;

  cmd.Parameters.Add("@Phone",SqlDbType.VarChar,50);
  cmd.Parameters["@Phone"].Value = this.phone;

  cmd.Parameters.Add("@Address",SqlDbType.VarChar,50);
  cmd.Parameters["@Address"].Value = this.address;

  string UserName=this.Request.Form["UserName"];
  string str = "select count(*) from[User] where UserName='"+UserName+"'";

  SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["yishilalaConnectionString"].ConnectionString);
  conn2.Open();
  SqlCommand cmd2 = new SqlCommand(str,conn2);
  int r=(int)cmd2.ExecuteScalar();
  conn2.Close();

  if (r >= 1)
  {
  Response.Write("<script>alert('用户已存在,请重新输入!')</script>");
  }
  else
  {
  try
  {
  cmd.ExecuteNonQuery();
  Page.Controls.Clear();
  Response.Write("<script>alert('注册成功!')</script>");
  }
  catch (Exception ex)
  {
  //throw new System.Exception(error.Message);
  }
  }
  }
}

这段代码没有显示错误,在注册时为什么没有值传到数据库,求大虾们指教!!

------解决方案--------------------
cmd 根本没有执行ExecuteNonQuery
------解决方案--------------------
语句错误,应该进入了异常
insert into [User](UserName,Password,Password2,Sex,Email,Phone,PostCode,Address)value(@UserName,@Psaaword,@Password2,@Sex,@Email,@Phone,@PostCode,@Address)

=》

insert into [User](UserName,Password,Password2,Sex,Email,Phone,PostCode,Address)values(@UserName,@Psaaword,@Password2,@Sex,@Email,@Phone,@PostCode,@Address
------解决方案--------------------
这句话前面最好加个@
conn.ConnectionString = "Data Source=.;Initial Catalog=yishilala;Integrated Security=True";
变成