static public SqlConnection conn
{
get
{
StreamReader file = new StreamReader("d:\\sqlcnf.txt");
string source = file.ReadToEnd();
return new SqlConnection(source);
}
}
static void Main(string[] args)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from [MyJob]", conn);
SqlDataReader reader = cmd.ExecuteReader();
...
然后就会出错
Unhandled Exception: System.InvalidOperationException: ExecuteReader requir open and available Connection. The connection's current state is closed.
如果我返回一个连接字符,串然后建立连接就没问题,这是为什么
{
get
{
StreamReader file = new StreamReader("d:\\sqlcnf.txt");
string source = file.ReadToEnd();
return new SqlConnection(source);
}
}
static void Main(string[] args)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from [MyJob]", conn);
SqlDataReader reader = cmd.ExecuteReader();
...
然后就会出错
Unhandled Exception: System.InvalidOperationException: ExecuteReader requir open and available Connection. The connection's current state is closed.
如果我返回一个连接字符,串然后建立连接就没问题,这是为什么
解决方案 »
- PropertyGrid的list属性弹出对话框
- c# 高人哪里有文本框中如何下拉出数据树的源码下载?
- radioButton怎么用啊
- 请问一下C#调用C++编写的(Standard Runtime Library DLL)dll出异常,如何解决?
- 使用ListView的detail显示时,可不可以直接通过点击listview中的表格,现实更改数据?
- C# iso8583解包和组包
- 随即生成字符串问题
- 很多问题 请帮助!
- 用代码连接数据库的问题。请走过路过的朋友进来看看
- C# DataTable 合并并且累加数据结果
- java用httpClient post方式传数据,ASP.NET怎么接收数据?
- 问一个有关HttpWebRequest和WebBrowser之间的小问题。
即使你要用
也应该是定义一个全局的static readonly string source = new StreamReader("d:\\sqlcnf.txt").ReadToEnd(); SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
//....
}