SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();
myConnection.Close();以上程序运行时总提示“SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);”这一行有错!未处理的“System.InvalidOperationException”类型的异常出现在 system.data.dll 中其他信息:连接已经打开 (state=Open)。已保证mySelectQuery, myConnection两项是正确的。大家帮忙看看问题何在?
SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
myReader.Close();
myConnection.Close();以上程序运行时总提示“SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);”这一行有错!未处理的“System.InvalidOperationException”类型的异常出现在 system.data.dll 中其他信息:连接已经打开 (state=Open)。已保证mySelectQuery, myConnection两项是正确的。大家帮忙看看问题何在?
解决方案 »
- 求教C#怎样将字符串中的16进制数据直接存入byte类型数组中
- 来一个最简单的Form与webService传值的例子
- 如何判断Form上多个ComboBox的选择项是否改变过?
- 无法找到表0
- 判断某个字符在集合有现成的函数吗
- 我要在WEB页面显示数据库里的一个表 而且是实事更新的 怎么做
- 单点登录的测试用例(或则是测试思想 安全方面)
- asp与asp.net共享session对话,请高手指教!
- 紧急!!!c#注册tomcat
- C# 的Linq提供器 谁有Demo或者给我提示下
- 急!!!!!关于播放视频组建问题?就剩90分了!!!请高手指点!!!!
- 如何把任意一个小于100的整数,转换成八位,01表示的字符串
=============>if (myConnection.State.ToString()=="Closed")
myConnection.Open();
CommandBehavior.CloseConnection
这样
using(SqlDataReader myReader = myCommand.ExecuteReader()) {
while(myReader.Read())
{
Console.WriteLine(myReader.GetString(0));
}
}
conn.Close();