简体   繁体   English

使用asp.net C#将数据插入数据库

[英]inserting data into database using asp.net c#

Am trying to insert data into a local database from an asp.net page using the code below but i keep getting this error 我正在尝试使用以下代码从asp.net页将数据插入本地数据库,但我一直收到此错误

"Incorrect syntax near the keyword 'user'" “关键字'用户'附近的语法不正确”

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
        con.Close();
        string inse = "insert into user (username, password, emailadd, fullname, country) values(@username, @password, @emailadd, @fullname, @country) ";
        SqlCommand insertuser = new SqlCommand(inse, con);
        insertuser.Parameters.AddWithValue("@username",TextBoxFA.Text);
        insertuser.Parameters.AddWithValue("@password", TextBoxEA.Text);
        insertuser.Parameters.AddWithValue("@emailadd", TextBoxRPW.Text);
        insertuser.Parameters.AddWithValue("@fullname", TextBoxPW.Text);
        insertuser.Parameters.AddWithValue("@country",DropDownList1.SelectedItem.ToString());

        try
        {
            insertuser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("login.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<b>something really bad happened.....Please try again</b> ");
        }
    }

Congratulations on using a parameterized query! 祝贺您使用参数化查询!

user is a keyword, so wrap it in square brackets, like [user] . user是一个关键字,因此请将其包装在方括号中,例如[user]

Some comments: 一些评论:

  1. You should use using for connection and command to dispose of unused resources automatically 您应该使用using用于连接和命令来自动处理未使用的资源
  2. The first con.Close(); 第一个con.Close(); doesn't make sense and can be removed. 没有意义,可以删除。 Instead you need to call con.Open(); 相反,您需要调用con.Open();
  3. Create a finally block where you close the connection. 在您关闭连接的位置创建一个finally块。 Currently it is not closed when an exception occurs. 当前,当发生异常时,它不会关闭。

That being said, your code then would read: 话虽如此,您的代码将显示为:

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString))
{
    con.Open();

    string inse = "insert into [user] (username, password, emailadd, fullname, country) values(@username, @password, @emailadd, @fullname, @country)";
    using (SqlCommand insertuser = new SqlCommand(inse, con))
    {
        insertuser.Parameters.AddWithValue("@username",TextBoxFA.Text);
        insertuser.Parameters.AddWithValue("@password", TextBoxEA.Text);
        insertuser.Parameters.AddWithValue("@emailadd", TextBoxRPW.Text);
        insertuser.Parameters.AddWithValue("@fullname", TextBoxPW.Text);
        insertuser.Parameters.AddWithValue("@country",DropDownList1.SelectedItem.ToString());

        try
        {
            insertuser.ExecuteNonQuery();
            Response.Redirect("login.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<b>something really bad happened.....Please try again</b> ");
        }
        finally
        {
            con.Close();
        }
    }
}

Try wrapping the word user in square brackets. 尝试将用户一词包装在方括号中。 I believe user is a reserved keyword. 我相信用户是保留关键字。

ie

string inse = "insert into [user] (username, password, emailadd, fullname, country) values(@username, @password, @emailadd, @fullname, @country) ";

You have a couple of other issues in that code, but not putting user in square brackets is what is causing the error message you are seeing. 您在该代码中还有其他几个问题,但是未将用户放在方括号中是导致您看到的错误消息的原因。

user是一个保留关键字 ,因此您只需将其包装在方括号中即可清楚表明您的意思是名为“ User”的对象:

insert into [user]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM