-1

4200 Syntax ErrorMSAccessデータベースに対してこのコードを実行すると次のようになります。

protected void Button1_Click(object sender, EventArgs e)
    {
        using (OdbcConnection conn = new OdbcConnection(@"Dsn=ani;dbq=D:\anita\inventory\chemicals.accdb;defaultdir=D:\anita\inventory;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin"))
        {
          conn.Open();
          string CommandText = "INSERT INTO SupplierDetails (ID, Supplier, Company, Address, State, Country, Pincode, PhoneNo, MobileNo, Email, Fax, RawMaterials, Note) VALUES (@ID, @Supplier, @Company, @Address, @State, @Country, @Pincode, @PhoneNo, @MobileNo, @Email, @Fax, @RawMaterials, @Note)";

            using (OdbcCommand cmd = new OdbcCommand(CommandText, conn))
            {
                cmd.Parameters.AddWithValue("@ID", TextBox3.Text);
                cmd.Parameters.AddWithValue("@Supplier", TextBox4.Text);
                cmd.Parameters.AddWithValue("@Company", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Address", TextBox11.Text);
                cmd.Parameters.AddWithValue("@State", TextBox2.Text);
                cmd.Parameters.AddWithValue("@Country", TextBox5.Text);
                cmd.Parameters.AddWithValue("@Pincode", TextBox10.Text);
                cmd.Parameters.AddWithValue("@PhoneNo", TextBox6.Text);
                cmd.Parameters.AddWithValue("@MobileNo", TextBox7.Text);
                cmd.Parameters.AddWithValue("@Email", TextBox8.Text);
                cmd.Parameters.AddWithValue("@Fax", TextBox9.Text);
                cmd.Parameters.AddWithValue("@RawMaterials", TextBox12.Text);
                cmd.Parameters.AddWithValue("@Note", TextBox13.Text);
                cmd.ExecuteNonQuery();
            }
        }
    }
4

1 に答える 1

6

名前の付いたフィールドは、JET4.0の予約済みキーワードNOTEと同じ名前です。 角かっこでカプセル化します

string CommandText = "INSERT INTO SupplierDetails (ID, Supplier, Company, Address, " + 
                     "State, Country, Pincode, PhoneNo, MobileNo, Email, Fax, " + 
                     "RawMaterials, [Note]) VALUES (@ID, @Supplier, @Company, @Address, " + 
                     "@State, @Country, @Pincode, @PhoneNo, @MobileNo, @Email, " + 
                     "@Fax, @RawMaterials, @Note)";

編集あなたが使用しているOdbcConnectionようです。これには、パラメータのプレースホルダーが、@プレフィックス付きの文字列ではなく、疑問符を使用して提供される必要があります。したがって、CommandTextは次のようになります。

string CommandText = "INSERT INTO SupplierDetails (Supplier, Company, Address, " + 
                     "State, Country, Pincode, PhoneNo, MobileNo, Email, Fax, " + 
                     "RawMaterials, [Note]) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

IDフィールドとそのパラメータープレースホルダーも削除したことに注意してください。これは、AutoIncrementフィールドであるため、独自の値を渡すことができないためです。また、このシナリオでは、パラメーターはパラメーターコレクション内の位置によって認識されることにも注意してください。したがって、commandTextで期待される正しい順序でそれらを追加することが非常に重要です。

于 2013-03-24T09:02:24.677 に答える