连接数据库为什么抛出异常

liusahara 2011-08-07 10:55:08

public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection
SqlDataReader reader = null;
SqlConnection cn = new SqlConnection(connectionString);
try
{
cn.Open();
//call the private overload that takes an internally owned connection in place of the connection string
reader = ExecuteReader(cn, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
}
catch
{
//if we fail to return the SqlDatReader, we need to close the connection ourselves
cn.Close();
MyDB._Check_Net = true;
}
return reader;
}

cn.Open();执行这句时抛出“无效操作,连接被关闭”的异常?这是为什么?连接字符串我敢肯定是正确的
...全文
193 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dllcash 2011-08-07
  • 打赏
  • 举报
回复
SqlDataReader reader = null;
这样能连上???
LMAOhuaNL 2011-08-07
  • 打赏
  • 举报
回复
sql服务开了没
liusahara 2011-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bjgzxx 的回复:]
网络是不是有问题呢
[/Quote]我连的是本地数据库
liusahara 2011-08-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 andywangguanxi 的回复:]
应该还是连接字符串的问题。你试试看能不能连上
此消息通过 【CSDN论坛 Winform测试版】 回复!
[/Quote]
解决了,谢谢!是连接字符串的问题,在server后面还加了个1433的端口号,多此一举!
EdsionWang 2011-08-07
  • 打赏
  • 举报
回复
应该还是连接字符串的问题。你试试看能不能连上 此消息通过 【CSDN论坛 Winform测试版】 回复!
liusahara 2011-08-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dllcash 的回复:]
SqlDataReader reader = null;
这样能连上???
[/Quote]

reader后面有返回对象给它啊,这里应该没什么问题吧
liusahara 2011-08-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lmaohuanl 的回复:]
sql服务开了没
[/Quote]
我连的是sql2005

111,093

社区成员

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

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

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