フレームワーク 4.0 と SQL サーバー 2008 R2 で C# を使用しています。私のプログラムは小さなデータでは問題なく動作しますが、データが巨大になるとタイムアウト例外が発生します。私は試してみました:
public void CreateSqlCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
}
問題はタイムアウトを増やす方法ではなく、プログラムを最適化するにはどうすればよいかということです。
2列のみを含む6つのテーブルLS_CLIENT_INSEE_A、LS_CLIENT_INSEE_Bなどを取得しました
目的は単純です。既存の行をコピーしたいだけですが、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); }
}
}
そのため、最初に SELECT してから INSERT INTO にする代わりに、両方を同時に実行して、TimeOutException とメモリ不足の問題を回避できます。
よろしくお願いします。