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附近有语法错误,一直不能往数据库里面添加记录,我检查了命令没发现有错误啊?这到底是怎么回事?已经发过一遍了,可是答案我试了都不对,不知道为什么

解决方案 »

  1.   

                 string sql = "insert into [dbo.User] (UserName,UserId,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";另外,最好用paramater方式
      

  2.   

    你可以吧sql语句放到查询分析器里执行下,看看有没有错
      

  3.   

    把SQL语句放到查询分析器试验一下,那样子可能容易发现问题,我看不出那句子有什么问题,是不是空格的问题,在该放空格的地方加几个空格看看吧,我以前碰到过空格问题
      

  4.   

    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 == "") 
        { 
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>alert('用户名或者密码不能为空')</script>");
        } 
        else 
        { 
            try 
            {
                string sql = "insert into [User] (UserName,UserId,Email) values(@UserName,@UserId,@Email)"; 
                SqlCommand cmd = new SqlCommand(sql, thisconnection);
                cmd.Parameters.AddWithValue("@UserName", TextBox1.Text);
                cmd.Parameters.AddWithValue("@UserId", TextBox2.Text);
                cmd.Parameters.AddWithValue("@Email", TextBox3.Text);
                thisconnection.Open(); 
                cmd.ExecuteNonQuery();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/JavaScript'>windows.location.href='Index.aspx';alert('注册成功'); </script>");
            } 
            catch(SqlException ee) 
            { 
                Response.Write(ee.Message); 
                //Response.Write(" <script>alert('数据库读取错误,请重新输入') </script>"); 
            } 
            finally 
            { 
                thisconnection.Close(); 
            } 
        } 
    } 如果有错,报什么错误信息
      

  5.   

    User附近有语法错误,报错是这样的
      

  6.   

    去掉DBO,试试
    应该是这个问题吧 
      

  7.   

    报的错误是:'User附近有语法错误'
      

  8.   

    把你抓取到的sql 語句發來一看就知道了`~
      

  9.   

    USER是关键字,
    错误
    SELECT * FROM DBO.USER
    正确:
    SELECT * FROM DBO.[USER]
      

  10.   

    human_2说的吧user改一下,改成dbo.[user] 我试了 不行啊
      

  11.   

    SELECT * FROM [USER]
    这样试过吗
      

  12.   

    insert into dbo.[user](User_Name,User_NickName,User_Pwd,User_Integral) values('111','wo','111','1')
    insert into [user](User_Name,User_NickName,User_Pwd,User_Integral) values('111','wo','111','1')
    insert into NewHotel(你的數據庫名字).dbo.[user](User_Name,User_NickName,User_Pwd,User_Integral) values('111','wo','111','1')完全可以執行  你設置斷點  將你的sql 語句 複製出來看看
      

  13.   

     string sql = "insert into [User] ([UserName],UserId,Email) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";