我使用的是SQL Server数据库,然后将SQL数据库导出到MS Access并在C#项目中使用该数据库。
现在当我尝试在任何表中插入或更新数据时,它会显示有关错误查询的错误消息。这是" INSERT INTO语句中的语法错误。"
我在C#中的查询是:
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Engr. Mac\Documents\Visual Studio 2012\Projects\SGM\SGM\SGdb.mdb");
OleDbCommand com = new OleDbCommand("", con);
con.Open();
com.CommandText = "INSERT INTO newOrder (Order_No, Customer_No, Issue_Date, Delivery_Date, Order_Type,Total_Weight) Values('" + orderbol.OrderNo + "', '" + orderbol.CustomerN0 + "', '" + orderbol.IssueDate + "', '" + orderbol.DlvryDate + "', '" + orderbol.OrdrType + "','"+0+"')";
if(com.ExecuteNonQuery()>0)
{
con.Close();
return true;
}
答案 0 :(得分:0)
使用命令参数而不是连接字符串是一个很好的做法,你可以在这里找到一个例子:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter(v=vs.110).aspx
但是,请尝试在您的示例中添加:
com.CommandText = "INSERT INTO newOrder (Order_No, Customer_No, Issue_Date, Delivery_Date, Order_Type,Total_Weight) Values(@1,@2,@3,@4,@5,@6);";
com.Parameters.Add("@Order_No", OleDbType.Char, 1).Value = "1";
com.Parameters.Add("@Customer_No", OleDbType.Char, 1).Value = "2";
com.Parameters.Add("@Issue_Date", OleDbType.Char, 1).Value = "3";
com.Parameters.Add("@Delivery_Date", OleDbType.Char, 1).Value = "4";
com.Parameters.Add("@Order_Type", OleDbType.Char, 1).Value = "5";
com.Parameters.Add("@Total_Weight", OleDbType.Char, 1).Value = "6";
并更改每个参数的数据类型
答案 1 :(得分:0)
这一直对我有用
对于db
,我将其放在我的应用程序的同一目录中,但您的可能会有所不同,因此请根据您的设置更改db
的值。
至于sql语句,我只是为你提供了一个简单的例子,根据你的表列替换sql语句以及代码cmd.Parameters.AddWithValue
的部分,因为在我的示例代码中,我是使用fruit
只有两列的表格,id
列为自动增量,水果名称为fruitname
列。
string db = Application.StartupPath + "\\Database1.accdb";
string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + db;
OleDbConnection c = new OleDbConnection(cs);
try
{
c.Open();
string s = "insert into fruit (fruitname) values (@fruitname)";
using (OleDbCommand cmd = new OleDbCommand(s, c))
{
cmd.Parameters.AddWithValue("@fruitname", textBox1.Text.Trim());
cmd.ExecuteNonQuery();
}
}
catch (Exception)
{
throw;
}
finally
{
c.Close();
}
答案 2 :(得分:0)
//插入示例
string _filename = @“C:\ Automation \ file.xls”;
_conn = "Provider=Microsoft.Ace.OLEDB.12.0;" + @"Data Source=" + _filename4 + ";" + "Extended Properties=Excel 8.0;";
_connection = new System.Data.OleDb.OleDbConnection(_conn);
// Use OledbCommand object to select all the data from sheet1 and execute a ExecuteNonQuery to import data into Book.mdb.
_command = new System.Data.OleDb.OleDbCommand();
_command.Connection = _connection;
_command.CommandText = @"insert into [MS Access;Database=C:\Automation\file1.accdb].[tab1] select [f1],[f2],[f3],[f4] FROM [Sheet0$]";
_connection.Open();
_command.ExecuteNonQuery();
_connection.Close();
//更新示例
connstr = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Automation\\IPR\\ipr.accdb";
conn = new System.Data.OleDb.OleDbConnection(connstr);
cmdBulk = new System.Data.OleDb.OleDbCommand("update tab1,tab2 set tab1.f1 = tab2.f2 where tab1.f2 = tab2.f3", conn);
conn.Open();
cmdBulk.ExecuteNonQuery();
conn.Close();