我正在使用带有框架 4.0 和 SQL Server 2008 R2 的 C#。我的程序适用于小数据,但是当数据变大时,我得到了超时异常。我试过:
public void CreateSqlCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
}
但它仍然挂断,我在资源池“默认”中获得了不足的系统内存来运行此查询,问题不是如何增加超时,而是如何优化我的程序。
我有 6 个表 LS_CLIENT_INSEE_A、LS_CLIENT_INSEE_B 等,其中仅包含 2 列
目标很简单,我只想复制现有行但使用不同的 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); }
}
}
所以也许不是先选择然后再插入,我可以同时做这两件事,以避免 TimeOutException 和内存不足问题。
提前谢谢你。