真郁闷,这是啥错误啊

mkshaoshao 2008-04-08 10:44:21
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataReader
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection("persist security info=true;password=11;user id=ggg521;initial catalog=northwind;data source=127.0.0.1;");
thisConnection.Open();
SqlCommand thisCommand = new SqlCommand();
thisCommand.CommandText = "select * from Customers";
SqlDataReader thisDataReader = thisCommand.ExecuteReader(); while (thisDataReader.Read())
{
Console.WriteLine("\t{0} \t{1}", thisDataReader[0], thisDataReader[1]);
}
thisDataReader.Close();
thisConnection.Close();
Console.ReadKey();

}
}
}
到了红的那就........
未处理的“System.InvalidOperationException”类型的异常出现在 System.Data.dll 中。

其他信息: ExecuteReader: Connection 属性尚未初始化。
...全文
64 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mkshaoshao 2008-04-08
  • 打赏
  • 举报
回复
3q
北京的雾霾天 2008-04-08
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataReader
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection("persist security info=true;password=11;user id=ggg521;initial catalog=northwind;data source=127.0.0.1;");
thisConnection.Open();
SqlCommand thisCommand = new SqlCommand();
thisCommand.Connection = thisConnection;//加上这一句
thisCommand.CommandText = "select * from Customers";
SqlDataReader thisDataReader = thisCommand.ExecuteReader(); while (thisDataReader.Read())
{
Console.WriteLine("\t{0} \t{1}", thisDataReader[0], thisDataReader[1]);
}
thisDataReader.Close();
thisConnection.Close();
Console.ReadKey();

}
}
}
userwjg 2008-04-08
  • 打赏
  • 举报
回复
thisCommand.Connection=thisConnection ;
mkshaoshao 2008-04-08
  • 打赏
  • 举报
回复
Visual Studio 调试器
关于异常的疑难解答:System.InvalidOperationException

当调用方法失败不是由无效参数所引起时,会引发 InvalidOperationException 异常。此异常可在无法调用基础 Win32 方法时由 .NET Framework 方法引发。

相关提示
如果在使用 GetHdc 方法后使用 Graphics 对象,请调用 ReleaseHdc 方法。
这将释放上一次调用 Graphics 对象的 GetHdc 方法时所获得的上下文句柄。

TableAdapters 需要有效的 UPDATE、INSERT 和 DELETE 命令以执行成功的保存操作。
TableAdapters 根据输入向导的原始 SELECT 语句生成 INSERT、UPDATE 和 DELETE 语句。某些情况下,对于成功保存所需要的 INSERT、UPDATE 和 DELETE 命令,SELECT 语句不会为适配器提供足够的信息以生成创建这些命令所必需的命令。这通常发生在数据库表没有主键列时,或 SELECT 命令使用 JOINS 时。有关更多信息,请参见如何:编辑 TableAdapter。
userwjg 2008-04-08
  • 打赏
  • 举报
回复
COMMAND没有连接对象
hackztx 2008-04-08
  • 打赏
  • 举报
回复

SqlConnection thisConnection = new SqlConnection("persist security info=true;password=11;user id=ggg521;initial catalog=northwind;data source=127.0.0.1;");
thisConnection.Open();
SqlCommand thisCommand = new SqlCommand("select * from Customers",thisConnection);//最简单的写法
SqlDataReader thisDataReader = thisCommand.ExecuteReader(); while (thisDataReader.Read())
{
Console.WriteLine("\t{0} \t{1}", thisDataReader[0], thisDataReader[1]);
}
thisDataReader.Close();
thisConnection.Close();
Console.ReadKey();
mkshaoshao 2008-04-08
  • 打赏
  • 举报
回复
我的CONNECTION是正确的,要不也打不开

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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