简体   繁体   English

如何在SQL Server 2008 R2中复制行

[英]How can i copy a row in sql server 2008 r2

i am using C# with framework 4.0 and SQL server 2008 R2. 我将C#与Framework 4.0和SQL Server 2008 R2配合使用。 my program works fine with a small data, but when the data become huge i got timeout Exception. 我的程序可以处理少量数据,但是当数据变大时,我会收到超时异常。 i have tried with: 我尝试过:

public void CreateSqlCommand()
{
   SqlCommand cmd = new SqlCommand();
   cmd.CommandTimeout = 0;
   cmd.CommandType = CommandType.Text;
}

but it still hang up, i got Insufficient system memory in resource pool 'default' to run this query, the question is not how to increase the time out, but how can i optimized my programme. 但它仍然挂断,我在资源池“默认”中没有足够的系统内存来运行此查询,问题不是如何增加超时时间,而是如何优化程序。

i got 6 table LS_CLIENT_INSEE_A, LS_CLIENT_INSEE_B, etc with conatain only 2 column 我有6个表LS_CLIENT_INSEE_A,LS_CLIENT_INSEE_B等,仅包含2列 在此处输入图片说明

the objective is simple, i just want to copy the existing row but with different NO_CLIENT. 目标很简单,我只想复制现有行,但使用不同的NO_CLIENT。

     public void CopyInsee(string SourceClient, List<object> DestClient)
             {
                 List<object> List_A = oClInse.GetInseClient("LS_CLIENT_INSEE_A", SourceClient);
                 List<object> List_B = oClInse.GetInseClient("LS_CLIENT_INSEE_B", SourceClient);
                 List<object> List_C = oClInse.GetInseClient("LS_CLIENT_INSEE_C", SourceClient);
                 List<object> List_D = oClInse.GetInseClient("LS_CLIENT_INSEE_D", SourceClient);
                 List<object> List_N = oClInse.GetInseClient("LS_CLIENT_INSEE_N", SourceClient);
                 List<object> List_P = oClInse.GetInseClient("LS_CLIENT_INSEE_P", SourceClient);
                 List<object> List_N1 = oClInse.GetInseClient("LS_CLIENT_INSEE_N1", SourceClient);
                 List<object> List_N2 = oClInse.GetInseClient("LS_CLIENT_INSEE_N2", SourceClient);
                 List<object> List_N3 = oClInse.GetInseClient("LS_CLIENT_INSEE_N3", SourceClient);
                 List<object> List_N4 = oClInse.GetInseClient("LS_CLIENT_INSEE_N4", SourceClient);
                 List<object> List_N5 = oClInse.GetInseClient("LS_CLIENT_INSEE_N5", SourceClient);
                 List<object> List_N6 = oClInse.GetInseClient("LS_CLIENT_INSEE_N6", SourceClient);

                 foreach (var oItem in DestClient)
                 {
                     if (List_A.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_A", oItem.ToString(), List_A);
                     if (List_B.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_B", oItem.ToString(), List_B);
                     if (List_C.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_C", oItem.ToString(), List_C);
                     if (List_D.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_D", oItem.ToString(), List_D);
                     if (List_N.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N", oItem.ToString(), List_N);
                     if (List_P.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_P", oItem.ToString(), List_P);
                     if (List_N1.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N1", oItem.ToString(), List_N1);
                     if (List_N2.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N2", oItem.ToString(), List_N2);
                     if (List_N3.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N3", oItem.ToString(), List_N3);
                     if (List_N4.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N4", oItem.ToString(), List_N4);
                     if (List_N5.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N5", oItem.ToString(), List_N5);
                     if (List_N6.Count != 0) oClInse.RempliClientInse("LS_CLIENT_INSEE_N6", oItem.ToString(), List_N6);
                 }

             }

 public List<object> GetInseClient(string NomTable,  string IdClient)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();
                    string sql_SelectAll = "SELECT * FROM " + NomTable + " WHERE NO_CLIENT = @NO_CLIENT";
                    SqlCommand comm_SelectAll = new SqlCommand(sql_SelectAll, connectionWrapper.conn);
                    comm_SelectAll.Parameters.AddWithValue("@NO_CLIENT", IdClient);
                    List<object> oList = new List<object>();
                    SqlDataReader readerOne = comm_SelectAll.ExecuteReader();
                    while (readerOne.Read())
                    {
                        oList.Add(readerOne["NO_INSEE"].ToString());
                    }
                    readerOne.Close();
                    readerOne.Dispose();
                    return oList;
                }
            }
            catch (Exception excThrown)
            {
                if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_GetAllUsrClient", excThrown); }
                else { throw new Exception(excThrown.Message, excThrown); }
            }

        }

public void RempliClientInse(string NomTable, string IdClient, List<object> InseList)
        {
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();

                    string sSql = "";

                    foreach (var oItem in InseList)
                    {
                        if (sSql != "") sSql += " UNION ALL ";
                        sSql += "SELECT '" + oItem.ToString() + "', '" + IdClient + "'";
                    }   
                    string TempSqlInst = "INSERT INTO " + NomTable + "  ( NO_INSEE, NO_CLIENT )  " + sSql;

                        SqlCommand comm_InsertMod = new SqlCommand(TempSqlInst , connectionWrapper.conn);
                        comm_InsertMod.CommandTimeout = 0;
                        comm_InsertMod.ExecuteNonQuery();

                }
            }
            catch (Exception excThrown)
            {
                if (!excThrown.Message.StartsWith("Err_")) { throw new Exception("Err_Domaine_RempliInsee", excThrown); }
                else { throw new Exception(excThrown.Message, excThrown); }
            }            
        }

so maybe instead of SELECT first then INSERT INTO, i can do both in the same time, to avoid TimeOutException and Memory insuffisante probleme. 因此,也许可以先执行SELECT而不是INSERT INTO的操作,然后同时执行两项操作,以避免TimeOutException和Memory insuffisante问题。

Thanks you in advance. 预先谢谢你。

I would use pure sql for that. 我会为此使用纯SQL。 Create stored procedure in SQL data base add it to your linq 2 sql project and execute it from the c# code. 在SQL数据库中创建存储过程,将其添加到linq 2 sql项目中,然后从c#代码执行它。

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

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