OleDbException(x80040E14):插入到语句中的语法错误

时间:2016-07-14 14:06:07

标签: c# oledb access insert-into

有人可以帮忙解决这个问题吗?我尝试了很多不同的事情,但我仍然遇到了这个错误。该错误指向cmd.ExecuteNonQuery()行。我现在已经检查过插入字符串一百万次,而且我不知道它有什么问题。谢谢!

OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + targetPath);                        
try
{
    for (int i = 0; i < listOfDBTables.Count; i++)
    {
        for (int j = 1; j < listOfDBTables[i].Rows.Count; j++)
        //Don't include the header
        {
            OleDbCommand cmd = new OleDbCommand("INSERT INTO POP(Global Customer, Sold To #, Material #, Material Description, Customer ID, Customer Name, City, State, Country, Market Code, Unit Cost, Invoice Date, [Month], Quantity, UoM, KG, [Year]) Values (@globCust,@soldToNum,@matNum,@matDesc,@custID,@custName,@city,@state,@country,@mktCode,@cost,@invDate,@month,@quantity,@uom,@kg,@year)", myConnection);

            cmd.Parameters.AddWithValue("@globCust", listOfDBTables[i].Rows[j][0].ToString());
            cmd.Parameters.AddWithValue("@soldToNum", listOfDBTables[i].Rows[j][1].ToString());
            cmd.Parameters.AddWithValue("@matNum", listOfDBTables[i].Rows[j][2].ToString());
            cmd.Parameters.AddWithValue("@matDesc", listOfDBTables[i].Rows[j][3].ToString());
            cmd.Parameters.AddWithValue("@custID", listOfDBTables[i].Rows[j][4].ToString());
            cmd.Parameters.AddWithValue("@custName", listOfDBTables[i].Rows[j][5].ToString());
            cmd.Parameters.AddWithValue("@city", listOfDBTables[i].Rows[j][6].ToString());
            cmd.Parameters.AddWithValue("@state", listOfDBTables[i].Rows[j][7].ToString());
            cmd.Parameters.AddWithValue("@country", listOfDBTables[i].Rows[j][8].ToString());
            cmd.Parameters.AddWithValue("@mktCode", listOfDBTables[i].Rows[j][9].ToString());
            cmd.Parameters.AddWithValue("@cost", listOfDBTables[i].Rows[j][10].ToString());
            cmd.Parameters.AddWithValue("@invDate", listOfDBTables[i].Rows[j][11].ToString());
            cmd.Parameters.AddWithValue("@month", listOfDBTables[i].Rows[j][12].ToString());
            cmd.Parameters.AddWithValue("@quantity", listOfDBTables[i].Rows[j][13].ToString());
            cmd.Parameters.AddWithValue("@uom", listOfDBTables[i].Rows[j][14].ToString());
            cmd.Parameters.AddWithValue("@kg", listOfDBTables[i].Rows[j][15].ToString());
            cmd.Parameters.AddWithValue("@year", listOfDBTables[i].Rows[j][16].ToString());

            myConnection.Open();
            cmd.ExecuteNonQuery();
        }
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}
finally
{
    myConnection.Close();
}

1 个答案:

答案 0 :(得分:0)

你应该把你的列放在[]中,例如把它[售给#]而不是卖给#