在 C# 中,我正在尝试将记录从一个访问表插入到另一个访问表,但我收到上述错误消息。 是什么导致了此错误(OleDbException:没有为一个或多个必需参数提供值),因为它是直接的 Select * 语句?
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=P:\Source.mdb;");
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO [;DATABASE=V:\Destination.mdb;].[table1] SELECT * FROM table1 WHERE company = 2", connection);
command.ExecuteNonQuery();
connection.Close();
编辑插入语句中的---错误
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=P:\Source.mdb;");
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO [;DATABASE=V:\Destination.mdb;].[table1] (Name, Address, Phone, RepeatCustomer) SELECT Name, Address, Phone, RepeatCustomer FROM table1 WHERE company = 2", connection);
command.ExecuteNonQuery();
connection.Close();
两个表相同吗?如果列顺序和/或类型不匹配,则会收到此错误。显式定义要修改的列是更好的形式:
INSERT INTO [;DATABASE=V:\Destination.mdb;].[table1] (col1,col2) SELECT col1,col2 FROM table1 WHERE company = 2
这样,顺序就无关紧要了,您就不会尝试添加到不存在的列中。
我在复制到同一个表时遇到了同样的问题。 这个只有两个字段的示例对我有用:
INSERT INTO [Orders] (CUST, PART_NUMBE) SELECT CUST, PART_NUMBE FROM [orders]
WHERE [OrderNumber]= 23979