1

以下のコードを使用して asp.net ページからローカル データベースにデータを挿入しようとしていますが、このエラーが発生し続けます

「キーワード '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> ");
        }
    }
4

3 に答える 3

6

パラメータ化されたクエリを使用しておめでとうございます!

userはキーワードなので、 のように角かっこで囲みます[user]

いくつかのコメント:

  1. using未使用のリソースを自動的に破棄するには、接続とコマンドに使用する必要があります
  2. 最初のcon.Close();ものは意味がなく、削除できます。代わりに、電話する必要がありますcon.Open();
  3. finally接続を閉じるブロックを作成します。現在、例外が発生したときに閉じられません。

そうは言っても、コードは次のようになります。

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();
        }
    }
}
于 2013-01-16T14:31:21.040 に答える
5

user という単語を角かっこで囲んでみてください。user は予約済みのキーワードだと思います。

すなわち

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

そのコードには他にもいくつかの問題がありますが、ユーザーを角括弧に入れていないことが、表示されているエラー メッセージの原因です。

于 2013-01-16T14:30:18.733 に答える
1

user予約済みのキーワードであるため、 「User」という名前のオブジェクトを意味することを明示するために、角括弧で囲む必要があります。

insert into [user]
于 2013-01-16T14:30:52.917 に答える