62,242
社区成员




源代码如下,请各位大佬帮忙
protected void Button1_Click(object sender, EventArgs e)
{
String strConn = @"Data Source=.;Initial Catalog=ynlyw;Integrated Security=true";
using (SqlConnection con = new SqlConnection(strConn))
{
con.Open();
string str = "select count(*) from User where UserName='" + TextBox1.Text + "'";
SqlCommand com = new SqlCommand(str, con);
int intcont = Convert.ToInt32(com.ExecuteScalar());
if (intcont > 0)//判断数据库中是否有相同的记录
{
Response.Write("alert('用户名已存在!')");
}
else
{
try
{
//插入命令
string sqlString = "insert into User(UserName,UPwd,Tel,QQ,Email) values(@p1,@p2,@p3,@p4,@p5)"; ;
//创建SqlCommand实例,并设置SQL语句和使用的连接实例
SqlCommand mycom = new SqlCommand(sqlString, con);
SqlParameter[] tvpParam = new SqlParameter[]
{
new SqlParameter("p1",TextBox1.Text),
new SqlParameter("p2",FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text,"MD5")),
new SqlParameter("p3",TextBox4.Text),
new SqlParameter("p4",TextBox5.Text),
new SqlParameter("p5",TextBox6.Text),
};
mycom.Parameters.AddRange(tvpParam);
mycom.ExecuteNonQuery();
Response.Write("good");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
}
不好意思,user是mssql的关键字,所以不可以直接在sql使用,如果要用请用方括号括起来
这样的问题,可以使用单步调试的方式进行排错,大部分情况下的错误是能够排除的,这个调试方法作为程序员来说是应该熟练掌握的。
会是因为数据库出错吗?之前用了另一个方法也是一样的错误,希望大家帮帮忙,谢谢大家