大家好,并提前致谢。我试图修复一个将信息插入数据库表的方法。目前它遇到超时,因为它在while循环中运行时间过长,无法处理所有内容。虽然我知道我只能增加命令超时,但我认为这不会解决问题,因为我认为它的代码。但我不确定正确的解决方案是什么。我可以访问Dapper,并且我想知道创建一个传递必要变量并为该组执行简单语句然后获取下一个变量的方法会更有效率吗?或者只是以不同的方式延续下面的内容?我是否应该将代码移出代码并将其转移到服务器上以获得更好的性能?超时过期 - 会话 - 如何修复
UPDATE全部错误消息:
类型 'System.Web.HttpUnhandledException' 引发的异常。
文件:C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \临时ASP.NET文件\ 6caa4c91 \ 19b853c6 \ App_Web_o3102kpb.9.cs
方法:的ProcessRequest行号:0
内部异常:{超时过期。操作完成之前超时的时间或服务器没有响应。
File:z:\ inetpub \ wwwroot \ SessionTransfer.aspx.cs
方法:AddSessionToDatabase行号:94
超时已过期。操作完成之前超时的时间或服务器没有响应。
文件:Z:\的Inetpub \ wwwroot的\ SessionTransfer.aspx.cs方法:的Page_Load行号:33}
这里是原代码:
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
int i = 0;
string strSql, guid = GetGuid();
string temp = "";
while (i < Session.Contents.Count)
{
if (Session.Contents[i] == null)
temp = "";
else {
if ((Session.Contents[i].ToString().Trim().Length) > 0)
temp = Session.Contents[i].ToString().Replace("'", "''");
else
temp = "";
}
strSql = "INSERT INTO SessionTable (GUID, SessionKey, SessionValue) " +
"VALUES ('" + guid + "', '" + Session.Contents.Keys[i].ToString() + "', '" + temp + "')";
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
i++;
}
con.Close();
cmd.Dispose();
con.Dispose();
return guid;
更新 - 最终解决方案:
var SessionList = new List<Session>();
while (i < Session.Contents.Count)
{
string temp = "";
if (Session.Contents[i] == null)
temp = "";
else
{
temp = (Session.Contents[i].ToString().Trim().Length) > 0 ? Session.Contents[i].ToString().Replace("'", "''") : "";
}
var s = new Session
{
TempGuid = guidTemp,
Contents = Session.Contents[i] != null ? Session.Contents[i].ToString() : null,
Temp = temp
};
SessionList.Add(s);
i++;
}
mySession = SerializationUtilities.SerializeObjectToXML(SessionList);
using (var con = new SqlConnection())
{
con.ExecuteHGW("Transfer", new { mySession }, commandType: CommandType.StoredProcedure);
}
然后在SQL身边,我只是把XML的一个表,并在对阵表中的一个单一的INSERT语句,时间显著改善。
第一个问题是超时消息实际上是从你的sql冒泡出来还是从执行查询所用的时间?是否可以包含实际的错误信息?这个循环有多少次? –
我已经包含了完整的错误消息,在这个特殊的例子中,Session.Contents.Count是38.我还有其他的错误,其中计数已经变化39,37等。 –
看起来像一个Web客户端正在等待响应服务器和超时。是否有可能在不同的线程中运行while循环并立即返回Guid? – MisterXero