求问各位,看看我的注册代码哪里出问题了,用户名和密码都可以判断成功,就是写不进数据库里,要怎么修改 protected void btnRegister_Click(object sender, EventArgs e)
{
//判断是否已存在用户
string strUser = tbusername.Text.Trim();
string strPsw = tbpsw.Text.Trim();
string strSql = string.Format("select * from tbUser where name='{0}'", strUser);
string connStr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = connStr;
sqlConn.Open();
SqlCommand cmd = new SqlCommand(strSql, sqlConn);
cmd.CommandType = CommandType.Text;
SqlDataReader sdr =cmd .ExecuteReader();
sdr.Read ();
DataTable dtUser = new DataTable();//执行SQL语句,并返回执行结果,转载到DataTable中
if (sdr.HasRows)
{
Response.Write(@"<script>alert('用户已存在,请重新输入');</script>");
}
else
{
//判断用户两次输入的密码是否一致
if (this.tbpsw.Text == this.tbpsw1.Text && this.tbpsw.Text != null)
{
sdr.Close();
string sql = string.Format("insert into tbUser(name,password)values ('" + tbusername.Text + "','" + tbpsw.Text + "')");
SqlCommand mycom = new SqlCommand(strSql, sqlConn);
mycom .ExecuteNonQuery();
sqlConn.Close();
sqlConn.Dispose();
Session["user"] = strUser;
Response .Write(@"<script>alert('注册成功!');</script>");
Response.Redirect("~/loading.aspx");
}
else
{
Response.Write(@"<script>alert('请确认两次输入密码一致!');</script>");
tbusername.Text = tbpsw.Text = string.Empty;
}
}
}