C#---MSSQL连接引起的问题。高手请进,速度结!

YSEE 2009-04-20 10:00:05
Source: System.Data
Message: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
Stack trace: 在 System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) 在 System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) 在 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable)

如上问题,如何解决!
...全文
158 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztmdsbt 2009-04-20
  • 打赏
  • 举报
回复
十楼正解!
zucc_sjj 2009-04-20
  • 打赏
  • 举报
回复
连接没关闭吧?
试下
conn1.Close();

YSEE 2009-04-20
  • 打赏
  • 举报
回复
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable)

这里可以看到我是用 DataAdapter 啊
YSEE 2009-04-20
  • 打赏
  • 举报
回复
我搜索了整个解决方案
都没有 DataReader 这样的字符串。那里来关闭啊。

引用的DLL我也搜索过了没啊。
chuxue1342 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ysee 的回复:]
可是我没有使用
DataReader 啊


我用的是

PrepareCommand(cmd1, conn1, null, cmdType, cmdText, cmdParms);
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = cmd1;
MyAdapter.Fill(ds, StartIndex, Size, TBname);

cmd1.Parameters.Clear();



[/Quote]
按提示是DataReader的问题,你是不是哪里使用了没有关闭,你再仔细检查一下!
chuxue1342 2009-04-20
  • 打赏
  • 举报
回复
提示已经很明显,你的sqlDataReader用完之后没有关闭,建议你加上这句:
SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection) //com是sqlcommand
csjtxy 2009-04-20
  • 打赏
  • 举报
回复
ding
YSEE 2009-04-20
  • 打赏
  • 举报
回复
可是我没有使用
DataReader 啊


我用的是

PrepareCommand(cmd1, conn1, null, cmdType, cmdText, cmdParms);
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = cmd1;
MyAdapter.Fill(ds, StartIndex, Size, TBname);

cmd1.Parameters.Clear();


return ds;
hanjinkai 2009-04-20
  • 打赏
  • 举报
回复
不太懂你的意思
jackljane 2009-04-20
  • 打赏
  • 举报
回复
DataReader 只能读一次。读过一次之后就不能回到指针的第一条记录位置。
kkun_3yue3 2009-04-20
  • 打赏
  • 举报
回复
关闭SqlDataReader

111,126

社区成员

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

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

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