我不知道如何在c#中執行此查詢。使用C將數據從一個SQL數據庫插入到第二個數據庫#
有兩個數據庫,每個都有一個這個查詢所需的表。我需要從一個數據庫表中獲取數據,並使用相應的payrollID更新其他數據庫表。
我有兩個表在單獨的數據庫中,員工在techData數據庫和strStaff在QLS數據庫中。在employee表中,我有StaffID,但需要從strStaff中提取PayrollID。
Insert payrollID into Employee where staffID from strStaff = staffID from Employee
但是我需要從strStaff獲得staffID和PayrollID,然後才能執行插入查詢。
這是我到目前爲止,但它不會工作。
cn.ConnectionString = ConfigurationManager.ConnectionStrings["PayrollPlusConnectionString"].ConnectionString;
cmd.Connection = cn;
cmd.CommandText = "Select StaffId, PayrollID From [strStaff] Where (StaffID = @StaffID)";
cmd.Parameters.AddWithValue("@StaffID", staffID);
//Open the connection to the database
cn.Open();
// Execute the sql.
dr = cmd.ExecuteReader();
// Read all of the rows generated by the command (in this case only one row).
For each (dr.Read()) {
cmd.CommandText = "Insert into Employee, where StaffID = @StaffID";
}
// Close your connection to the DB.
dr.Close();
cn.Close();
'對於每個'? o.O你的意思是'while'?並且應該在你的while語句中加入你的'@ StaffID'值並用'ExecuteNonQuery'執行它。你也應該考慮用'Parameters.Clear()'方法每次迭代清除你的參數。順便說一下,不清楚你想要插入的列值。你應該得到像'reader [0],reader [1] etc ..'這樣的值,你應該插入_that_值。 –
對不起,真的不好,在這一點。 – Redheadinferno
在循環中做兩件事:'while(dr。Read()){//獲取參數值//執行INSERT命令}'讓我們知道你被困在哪個部分 - 這裏有很多例子。 –