protected void Button1_Click(object sender, EventArgs e)
{
string connectstring = @"server=CSDN-3F5C293478\SQLEXPRESS;database=website;integrated security=true;";
SqlConnection thisconnection = new SqlConnection(connectstring);
if (TextBox1.Text == "" || TextBox2.Text == "")
{
Response.Write("<script>alert('用户名或者密码不能为空')</script>");
}
else
{
try
{
string sql = "insert into dbo.User (UserName,UserId,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
SqlCommand cmd = new SqlCommand(sql, thisconnection);
thisconnection.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>windows.location.href='Index.aspx';alert('注册成功');</script>");
}
catch(SqlException ee)
{
Response.Write(ee.Message);
//Response.Write("<script>alert('数据库读取错误,请重新输入')</script>");
}
finally
{
thisconnection.Close();
}
}
}
这是asp.net注册登录时的注册按钮的处理事件,连接数据库没有问题。但是当到了红色的这行时,捕捉到了异常,说是User附近有语法错误,一直不能往数据库里面添加记录,我检查了命令没发现有错误啊?这到底是怎么回事?
{
string connectstring = @"server=CSDN-3F5C293478\SQLEXPRESS;database=website;integrated security=true;";
SqlConnection thisconnection = new SqlConnection(connectstring);
if (TextBox1.Text == "" || TextBox2.Text == "")
{
Response.Write("<script>alert('用户名或者密码不能为空')</script>");
}
else
{
try
{
string sql = "insert into dbo.User (UserName,UserId,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
SqlCommand cmd = new SqlCommand(sql, thisconnection);
thisconnection.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>windows.location.href='Index.aspx';alert('注册成功');</script>");
}
catch(SqlException ee)
{
Response.Write(ee.Message);
//Response.Write("<script>alert('数据库读取错误,请重新输入')</script>");
}
finally
{
thisconnection.Close();
}
}
}
这是asp.net注册登录时的注册按钮的处理事件,连接数据库没有问题。但是当到了红色的这行时,捕捉到了异常,说是User附近有语法错误,一直不能往数据库里面添加记录,我检查了命令没发现有错误啊?这到底是怎么回事?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货