如何在foreach循环中处理Select,Update查询?

时间:2010-08-06 05:40:34

标签: c# sql-server excel

SERVER A:public string connStr =“Data Source = PH-09-5336; Initial Catalog = InventoryDB; Integrated Security = True”;

SERVER B:public string connWIP =“Data Source = PWODU-COGNOSDB3; Initial Catalog = BI_SOURCE;

我有这种方法从excell文件中将记录插入InventoryDB.DBO.FG_FILLIN。

从BI_SOURCE.dbo.ODU_WIP_PI中选择记录,然后更新InventoryDB.DBO.FG_FILLIN上的空记录(项目编号),如果其序列号在BI_SOURCE.dbo.ODU_WIP_PI serialnumber上匹配。

问题:

我尝试更新了13000条记录,需要5分钟才能更新。 我需要更新只有itemnumber具有空记录的新插入记录InventoryDB.DBO.FG_FILLIN。

但在我的代码中,它会循环并再次更新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 ItemNumber=@ItemNumber where SerialNumber=@SerialNumber", 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

2 个答案:

答案 0 :(得分:0)

如果我已正确理解您的问题,您可以通过执行单个SQL语句来大大加快更新过程,而不是单独循环遍历每一行。

这个怎么样?将记录加载到表A(FG_FILLIN)后,创建一个SQL UPDATE语句,该语句使用表A中的连接到表B,其中表A中的ItemNumber字段为空。这样的事情应该这样做:

UPDATE [dbo].[FG_FILLIN]
SET ItemNumber = table2.WIP_ItemID
FROM [dbo].[FG_FILLIN] INNER JOIN BI_SOURCE.dbo.ODU_WIP_PI as table2
ON [dbo].[FG_FILLIN].SerialNumber = table2.WIP_SerialNumber
WHERE [dbo].[FG_FILLIN].ItemNumber IS NULL

答案 1 :(得分:0)

另一件事, 你也可以将你的东西包装成

using(var scope = new TransactionScope())
{
stuff
scope.Complete();
}

使其成为交易