2011-11-24 72 views
0

我在使用Visual Stuido 2010 C#和的Microsft Access中创建一个应用程序是新的2007年,我打算创建一个应用程序,用户可以将数据添加到数据库( MS-访问)。但是,我得到了“语法错误(缺少操作员)在查询表达式”的错误提示。我真的找不到我的代码有什么问题。OleDbException是未处理:语法错误(缺少操作员)在查询表达式

这是我在将数据添加到数据库代码:

private void buttonSaveFuelLimit_Click(object sender, EventArgs e) 
    { 
     string MyConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\KKKKK\Documents\Visual Studio 2010\Projects\Trial\Trial\gxi.accdb"; 
     OleDbConnection connection = new OleDbConnection(MyConString); 
     OleDbCommand command = connection.CreateCommand(); 
     command.Connection = connection; 
     using (OleDbConnection conn = new OleDbConnection(MyConString)) 
     { 
      connection.Open(); 
      using (OleDbCommand com = connection.CreateCommand()) 
      { 
       command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?fuel_limit_code, ?fuel_limit_description)"; 
       command.Parameters.Add(new OleDbParameter("?fuel_limit_code", OleDbType.VarChar)); 
       command.Parameters.Add(new OleDbParameter("?fuel_limit_description", OleDbType.VarChar)); 
       command.Parameters["?fuel_limit_code"].Value = textBoxFuelLimitCode.Text; 
       command.Parameters["?fuel_limit_description"].Value = textBoxFuelLimitDesc.Text; 
       command.ExecuteNonQuery(); 
       MessageBox.Show("Data Saved"); 
      } 
     } 
    } 

这是错误消息的截屏: enter image description here

回答

1

---编辑答案--- 以下将工作


command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?, ?)"; 
command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_code",textBoxFuelLimitCode.Text)); 
command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_desc", textBoxFuelLimitDesc.Text)); 
command.ExecuteNonQuery(); 


---老答案---

这里是一个需要修正


command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description)"; 
//should ne 
command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?,?)"; 
+0

我已经在查询中添加了这些值,但发生了新的错误。这可能是什么问题。谢谢 – sean

+0

我刚刚编辑了我的问题。 – sean

+0

更新的答案,当我用你的代码,以及 –

4

你需要插入到语句的值的一部分。

INSERT INTO fuel_limit(fuel_limit_code,fuel_limit_description)值(?fuel_limit_code,?fuel_limit_description)

此外,它看起来像你的,你需要在右边的ExecuteNonQuery statment上述线路设定值时使用的描述参数。

+0

感谢行那。我已经添加了代码,但发生了新的错误。你认为我的代码有什么问题? – sean

+0

我刚刚编辑了我的问题。 – sean

相关问题