简体   繁体   English

从C#应用程序插入Access数据库失败

[英]insert into Access database failing from C# application

I have joined two table values and inserting those values in another table, but insertion is not working. 我已加入两个表值并将这些值插入另一个表中,但插入不起作用。 I am using an Access database, and I am using the following code: 我正在使用Access数据库,我使用以下代码:

 string query = "select t2.date,t1.FlightNo,t1.Dept_Time,t1.Arr_Time,t1.Route,t1.[Destination 1],t1.[Destination 2],t1.[Destination 3],t1.[Destination 4] From [FlightNumber]as t1 inner join [schedule]as t2 on t1.FlightNo=t2.FlightNo";

 OleDbCommand cmd = new OleDbCommand(query, con);

 OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
 dt1 = new DataTable();
 adp.Fill(dt1);
 con.Open();
 string query2 = "Insert into Monday (date,[Flight_no],[Dept_time],[Arr_time],Route,[dest_1],dest2,dest3,dest4)values(@date,@flight,@dept,@arr,@route,@dest1,@dest2,@dest3,@dest4)";
 for (int i = 0; i < dt1.Rows.Count; i++)
 {
     OleDbCommand cmd1 = new OleDbCommand(query2, con);

     cmd.Parameters.AddWithValue("@date", SqlDbType.NVarChar).Value = dt1.Rows[i]["date"].ToString();
     cmd.Parameters.AddWithValue("@flight", dt1.Rows[i]["FlightNo"]);
     cmd.Parameters.AddWithValue("@dept", dt1.Rows[i]["Dept_Time"]);
     cmd.Parameters.AddWithValue("@arr", dt1.Rows[i]["Arr_Time"]);
     cmd.Parameters.AddWithValue("@route", dt1.Rows[i]["Route"]);
     cmd.Parameters.AddWithValue("@dest1", dt1.Rows[i]["Destination 1"]);
     cmd.Parameters.AddWithValue("@dest2", dt1.Rows[i]["Destination 2"]);
     cmd.Parameters.AddWithValue("@dest3", dt1.Rows[i]["Destination 3"]);
     cmd.Parameters.AddWithValue("@dest4", dt1.Rows[i]["Destination 4"]);

     cmd1.ExecuteNonQuery();
     con.Close();
     MessageBox.Show("successfully inserted");            
 }

DATE是Access SQL中的保留字 ,因此您需要在INSERT命令中将该列名包装在方括号中(就像使用[Flight_no],[Dept_time]等)。

string query2 = "Insert into Monday ([date], ...

In addition to what Gord mentioned about reserved word Date, you might be better on performance if you don't keep rebuilding the command and parameters, especially if you have a lot of records you are trying to insert. 除了Gord提到的保留字Date之外,如果你不继续重建命令和参数,你可能会在性能方面做得更好,特别是如果你有很多想要插入的记录。 Instead, do something like... 相反,做一些像......

string query2 = "Insert into Monday (date,[Flight_no],[Dept_time],[Arr_time],Route,[dest_1],dest2,dest3,dest4)values(@date,@flight,@dept,@arr,@route,@dest1,@dest2,@dest3,@dest4)";

// Pre-build the command and parameters ONCE //预先构建命令和参数ONCE
OleDbCommand cmd1 = new OleDbCommand(query2, con); OleDbCommand cmd1 = new OleDbCommand(query2,con);

cmd.Parameters.AddWithValue("@date", dt1.Rows[i]["date"].ToString() ); cmd.Parameters.AddWithValue(“@ date”,dt1.Rows [i] [“date”]。ToString()); cmd.Parameters.AddWithValue("@flight", dt1.Rows[i]["FlightNo"]); cmd.Parameters.AddWithValue(“@ flight”,dt1.Rows [i] [“FlightNo”]); cmd.Parameters.AddWithValue("@dept", dt1.Rows[i]["Dept_Time"]); cmd.Parameters.AddWithValue(“@ dept”,dt1.Rows [i] [“Dept_Time”]); cmd.Parameters.AddWithValue("@arr", dt1.Rows[i]["Arr_Time"]); cmd.Parameters.AddWithValue(“@ arr”,dt1.Rows [i] [“Arr_Time”]); cmd.Parameters.AddWithValue("@route", dt1.Rows[i]["Route"]); cmd.Parameters.AddWithValue(“@ route”,dt1.Rows [i] [“Route”]); cmd.Parameters.AddWithValue("@dest1", dt1.Rows[i]["Destination 1"]); cmd.Parameters.AddWithValue(“@ dest1”,dt1.Rows [i] [“Destination 1”]); cmd.Parameters.AddWithValue("@dest2", dt1.Rows[i]["Destination 2"]); cmd.Parameters.AddWithValue(“@ dest2”,dt1.Rows [i] [“Destination 2”]); cmd.Parameters.AddWithValue("@dest3", dt1.Rows[i]["Destination 3"]); cmd.Parameters.AddWithValue(“@ dest3”,dt1.Rows [i] [“Destination 3”]); cmd.Parameters.AddWithValue("@dest4", dt1.Rows[i]["Destination 4"]); cmd.Parameters.AddWithValue(“@ dest4”,dt1.Rows [i] [“Destination 4”]);

 for (int i = 0; i < dt1.Rows.Count; i++)
 {
     cmd.Parameters[0].Value = dt1.Rows[i]["date"].ToString();
     cmd.Parameters[1].Value = dt1.Rows[i]["FlightNo"]);
     cmd.Parameters[2].Value = dt1.Rows[i]["Dept_Time"]);
     cmd.Parameters[3].Value = dt1.Rows[i]["Arr_Time"]);
     cmd.Parameters[4].Value = dt1.Rows[i]["Route"]);
     cmd.Parameters[5].Value = dt1.Rows[i]["Destination 1"]);
     cmd.Parameters[6].Value = dt1.Rows[i]["Destination 2"]);
     cmd.Parameters[7].Value = dt1.Rows[i]["Destination 3"]);
     cmd.Parameters[8].Value = dt1.Rows[i]["Destination 4"]);

     cmd1.ExecuteNonQuery();
 }

 con.Close();
 MessageBox.Show("successfully inserted");            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM