Reputation: 4261
i am using C# with framework 4.0 and 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
the objective is simple, i just want to copy the existing row but with different 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.
Thanks you in advance.
Upvotes: 0
Views: 142
Reputation: 449
I would use pure sql for that. Create stored procedure in SQL data base add it to your linq 2 sql project and execute it from the c# code.
Upvotes: 1