簡體   English   中英

C#中的insert into語句(0x80040E14)中的oledb異常語法錯誤

[英]oledb exception syntax error in insert into statement (0x80040E14) in C#

我在insert into語句中遇到了異常,但是數據正確地插入了表中。 有人可以告訴我此代碼中的錯誤。

private void btnAddNewSale_Click(object sender, EventArgs e)
{     
            string StrQuery;
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            for (int i = 0; i < DataGridViewAddSale.Rows.Count; i++)
            {
                StrQuery = "insert into BillItem (billNumber,storeItemNumber,numberOfItems,priceForEach,totalValue) values (" + txtBillNo.Text + ", "+ DataGridViewAddSale.Rows[i].Cells["ColCordNo"].Value + ", "+ DataGridViewAddSale.Rows[i].Cells["ColQty"].Value + ", " + DataGridViewAddSale.Rows[i].Cells["ColUnitPrice"].Value + " ," + DataGridViewAddSale.Rows[i].Cells["ColTotalValue"].Value + ");";
                command.CommandText = StrQuery;
                command.ExecuteNonQuery();
            }
        }

以下是例外

System.Data.OleDb.OleDbException (Ox8004OE14): Syntax error in INSERT INTO statement.
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OIeDbHResult br)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Oata.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OIeDb.OleDbCommand.ExecuteReaderlnternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQueryo
at Bsystem_1 ._1 .Form2.btnAddNewSale_Click(Object sender, EventArgs e) in c:\Users\jagath\Documents\Visual Studio 2013\Projects\Bsystem 1.1\Bsystem
1.1\Form2.cs:line 166
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, 1nt32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.BuftonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, 1nt32 msg, IntPtr wparam, IntPtr Ipa ram)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
dwComponentlD, 1nt32 reason, 1nt32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLooplnner(1nt32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(1nt32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialogO
at Bsystem_1 ._1 .Forml .btnjogin_Click(Object sender, EventArgs e) in c:\Users\jagath\Documents\Visual Studio 2013\Projects\Bsystem 1.1\Bsystem
1.1\Forml.cs:line 49

檢查列的名稱和數據類型。 然后使用CommandParameter避免SQL注入。 如果您使用保留字作為表名,我也建議您用方括號將表名封裝起來。

using (var command = new OleDbCommand
{
    Connection = connection,
    CommandText =
        "insert into [BillItem] (billNumber, storeItemNumber, numberOfItems, priceForEach, totalValue) values (?, ?, ?, ?, ?)"
})
{
    for (int i = 0; i < DataGridViewAddSale.Rows.Count; i++)
    {
        command.Parameters.Clear();
        var rowCells = DataGridViewAddSale.Rows[i].Cells;
        command.Parameters.AddWithValue("@p1", txtBillNo.Text);
        command.Parameters.AddWithValue("@p2", rowCells["ColCordNo"].Value);
        command.Parameters.AddWithValue("@p3", rowCells["ColQty"].Value);
        command.Parameters.AddWithValue("@p4", rowCells["ColUnitPrice"].Value);
        command.Parameters.AddWithValue("@p5", rowCells["ColTotalValue"].Value);

        command.ExecuteNonQuery();
    }
}

如上所述,您必須使用參數化查詢來保護sql-injection。

insert into [BillItem] (billNumber, storeItemNumber, numberOfItems, priceForEach, totalValue) 
values (@para1,@para2,@para3,...)

//and add into command
command.Parameters.AddWithValue("@para1", txtBillNo.Text);

當您的值具有Sqlserver保留字時 ,出現上述錯誤,很可能是單引號,例如charlie's angels或xml字段中的<或>或&

檢查在該表中是否分配了任何無效的外鍵(關系)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM