2013-10-19 114 views
0

我已經加入了兩個表值並將這些值插入到另一個表中,但插入不起作用。我使用的是Access數據庫,我使用下面的代碼:插入到C#應用程序失敗的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");    
} 
+1

解釋它是如何工作的。是否有編譯器錯誤,運行時異常,意外輸出或其他? –

回答

2

DATE是訪問SQL一個reserved word,所以你需要用該列名在方括號中的INSERT命令(像你這樣用[Flight_no],[Dept_time]等)。

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

除了什麼戈德提及保留字日期,你可能會在性能更好,如果你不保持重建的命令和參數,特別是如果你有很多你想插入記錄。相反,這樣做......

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)"; 

//預構建的命令和參數ONCE
OleDbCommand的CMD1 =新的OleDbCommand(QUERY2,CON);

cmd.Parameters.AddWithValue(「@ date」,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」]);

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");