请各位有空的帮我看看下面的代码,我的代码老实出现超时的错。请帮我改进一下。

postfxj 2008-06-13 09:29:45


public static byte[] getDataSetBySQL(string[] sqlstr, string[] namestr)//返回一個可以包括多個表的序列化並加壓後的數據集,
{
SqlConnection conn = new SqlConnection(connstring);//connsting中有timeoute=0;
conn.Open();
DataSet DS = new DataSet();
try
{
for (int i = 0; i < sqlstr.Length; i++)
{
SqlDataAdapter DA = new SqlDataAdapter(sqlstr[i], conn);
DA.Fill(DS, namestr[i]);

}
return SerializationDataset(DS);
}
catch
{
return null;
}
finally
{
conn.Close();
}
}

请帮我改改上面的代码?让它不再出现timeout;
...全文
185 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
postfxj 2008-06-18
  • 打赏
  • 举报
回复
我是在iis部署我的webservice,我的查询语句就是通过webservice来提交的。
我的连接配置timeou=0,执行seleccommand的 timeout=0我就不知还要在哪里设什么了。
反正最后还是返回操作超时。
postfxj 2008-06-16
  • 打赏
  • 举报
回复
现在问题是,返回出错时间基本上是执行成功后的时间。
postfxj 2008-06-16
  • 打赏
  • 举报
回复
cpu執行率不是問題,我的服務器是比較好的,cpu執行效率不是很高的。
隻是時間長一點,可能要2分多鐘吧。隻是查詢語句中有求和sum(),統計半年的歷史數據,統計出來的結果也就100來條記錄。
postfxj 2008-06-13
  • 打赏
  • 举报
回复
不是把timeou设为0就不受限制了。
postfxj 2008-06-13
  • 打赏
  • 举报
回复
顶一下
timeout 连接的地方我也改了,执行的地方我也改了。
postfxj 2008-06-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zmj1563 的回复:]
应该是查询超时把,VS有个默认的SQL执行时间 查询时间太长的话就超时了,把这个默认时间设置大一点看看
[/Quote]
请问在哪设呢?
不是这样设的吗?

SqlCommand cmd = new SqlCommand(sqlstr[i], conn);
cmd.CommandTimeout = 10000000;
SqlDataAdapter DA = new SqlDataAdapter(cmd);


lirongxj 2008-06-13
  • 打赏
  • 举报
回复
你这个查询应该是花很多时间的那种 所以要设置一下命令执行时间
SqlDataAdapter DA = new SqlDataAdapter(sqlstr[i], conn);
DA.SelectCommand.CommandTimeout = 500; //设置命令执行超时时间
试一下看看
postfxj 2008-06-13
  • 打赏
  • 举报
回复
我把查询语句提取出来在查询分析器中执行没有问题,只是花时有2分56秒。
postfxj 2008-06-13
  • 打赏
  • 举报
回复
我改成如下这样了,还是报错。

public static byte[] getDataSetBySQL(string[] sqlstr, string[] namestr)//返回一個可以包括多個表的序列化並加壓後的數據集,
{
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
string s = conn.ConnectionTimeout.ToString();
DataSet DS = new DataSet();
try
{
for (int i = 0; i < sqlstr.Length; i++)
{
SqlCommand cmd = new SqlCommand(sqlstr[i], conn);
cmd.CommandTimeout = 10000000;
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(DS, namestr[i]);
}
return SerializationDataset(DS);
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
zmj1563 2008-06-13
  • 打赏
  • 举报
回复
应该是查询超时把,VS有个默认的SQL执行时间 查询时间太长的话就超时了,把这个默认时间设置大一点看看
yjvjom 2008-06-13
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection(connstring);//connsting中有timeoute=0;
conn.Open();
DataSet DS = new DataSet();
try
{
for (int i = 0; i < sqlstr.Length; i++)
{
SqlDataAdapter DA = new SqlDataAdapter(sqlstr[i], conn);
DA.SelectCommand.CommandTimeOut=300;//如果出现超时错误,可能是查询返回的数据太多,把超时设置大点试试
DA.Fill(DS, namestr[i]);

}
return SerializationDataset(DS);//另外,最好不要在这里返回
}
catch
{
DS=null;
// return null;
}
finally
{
conn.Close();
}

return DS;
postfxj 2008-06-13
  • 打赏
  • 举报
回复
报错如下:the operation has time out.
postfxj 2008-06-13
  • 打赏
  • 举报
回复
其实这个只是一个公用的查询函数,只是查询语句可能花时较多,是超时了没错,都报错timeout.
要解决的就是不让它超时就好了。
Coco孙 2008-06-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cnapc 的回复:]
不一定是代码有问题,有可能是数据库服务器端的问题
[/Quote]
cnapc 2008-06-13
  • 打赏
  • 举报
回复
不一定是代码有问题,有可能是数据库服务器端的问题
cnapc 2008-06-13
  • 打赏
  • 举报
回复
在哪一句出的错?那时的i等于多少?你的数据库是用的什么?
zzyhuian06142 2008-06-13
  • 打赏
  • 举报
回复
如果实在要改DataSet的tablename的话,就只能在查询结束后
在循环一次并更改每个TableName
比如
for(int i=0;i <namestr.Length;i++)
{
DS.Table[i].TableName = namestr[i].ToString();
}
zzyhuian06142 2008-06-13
  • 打赏
  • 举报
回复
ADO可以一次执行多个Select语句,不需要一个一个去执行
还有cnn.Open()可以省略的
zzyhuian06142 2008-06-13
  • 打赏
  • 举报
回复
string _str="";
for(int i=0;i<sqlstr.Length;i++)
{
if(i<i<sqlstr.Length-1)
{
_str +=sqlstr[i] + ";";
}
else
{
_str +=sqlstr[i];
}
}
try
{
SqlDataAdapter DA = new SqlDataAdapter(_strconn);
DA.Fill(DS);

}
catch
{}
postfxj 2008-06-13
  • 打赏
  • 举报
回复
路过的请顶一下,谢谢。
加载更多回复(3)

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧