在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();
答案 0 :(得分:1)
两张桌子相同吗?如果列顺序和/或类型不匹配,则会出现此错误。明确定义要修改的列的形式要好得多:
INSERT INTO [;DATABASE=V:\\Destination.mdb;].[table1] (col1,col2) SELECT col1,col2 FROM table1 WHERE company = 2
这样,顺序无关紧要,并且您不会尝试添加到不存在的列。
答案 1 :(得分:0)
我有同样的问题复制到同一个表。这个只有两个字段的样本对我有用:
INSERT INTO [Orders] (CUST, PART_NUMBE) SELECT CUST, PART_NUMBE FROM [orders]
WHERE [OrderNumber]= 23979