SERVER A:public string connStr =「Data Source = PH-09-5336; Initial Catalog = InventoryDB; Integrated Security = True」;如何選擇,更新對foreach循環的查詢?
SERVER B:公共字符串connWIP =「數據源= PWODU-COGNOSDB3;初始目錄= BI_SOURCE;
我已經從EXCELL文件插入記錄到InventoryDB.DBO.FG_FILLIN的這種方法
選擇。從BI_SOURCE.dbo.ODU_WIP_PI記錄然後InventoryDB.DBO.FG_FILLIN更新空記錄(項目編號)如果SERIALNUMBER是匹配的BI_SOURCE.dbo.ODU_WIP_PI SERIALNUMBER
問題:
我曾嘗試更新了13000條記錄,需要5分鐘才能更新。 我需要更新新插入的記錄InventoryDB.DBO.FG_FILLIN,它只有itemnumber具有空記錄。
但在我的代碼,它循環和InventoryDB.DBO.FG_FILLIN
再次更新所有記錄我真的棧這個問題。
-------------------------------------INSERT RECORDS----------------------------------------
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand("Insert into dbo.FG_FILLIN Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=" + filepath1 + ";HDR=YES','SELECT * FROM [" + Sheetname1 + "$]')", conn))
{
try
{
conn.Open();
cmd.ExecuteNonQuery();
txtsheet1.Text = string.Empty;
txtpath1.Text = string.Empty;
txtpath1.Focus();
MessageBox.Show("FILL IN Mass Upload Success!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txtsheet1.Text = string.Empty;
txtpath1.Text = string.Empty;
txtpath1.Focus();
}
finally
{
if (conn.State == ConnectionState.Open) cmd.Connection.Close();
conn.Close();
}
}
---------------------------------SELECT UPDATE -----------------------------------
public void Update()
{
using (SqlConnection conn = new SqlConnection(connWIP))
{
try
{
conn.Open();
using (SqlDataAdapter dAd = new SqlDataAdapter("select WIP_serialNumber, WIP_ItemID from BI_SOURCE.dbo.ODU_WIP_PI", conn))
{
DataTable data = new DataTable();
dAd.Fill(data);
using (SqlConnection conn2 = new SqlConnection(connStr))
{
conn2.Open();
try
{
foreach (DataRow recordFromServerA in data.Rows)
{
using (SqlCommand dCmd = new SqlCommand("update [dbo].[FG_FILLIN] SET [email protected] where [email protected]", conn2))
{
dCmd.Parameters.AddWithValue("@ItemNumber", recordFromServerA["WIP_ItemAlias"]);
dCmd.Parameters.AddWithValue("@SerialNumber", recordFromServerA["WIP_serialNumber"]);
dCmd.ExecuteNonQuery();
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
if (conn2.State == ConnectionState.Open) conn2.Close();
}
}
}
MessageBox.Show("All Records Updated Successfully!");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
#endregion