之前编译的还可以通过,可是今天就不行了,总是出现问题,问题显示为:
未处理的“System.NullReferenceException”类型的异常出现在 system.data.dll 中。其他信息: 未将对象引用设置到对象的实例。总是中断在 SqlCommand thisCommand = thisConnection.CreateCommand(); 这句上面,不知是怎么回事,请达人指教啊!
下面是代码:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DataReading
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main()
{
SqlConnection thisConnection = new SqlConnection(@"Data Source = (local);Integrated Security = SSPI;" + "Initial Catalog = Northwind");

//打开连接
thisConnection.Open(); //创建命令对象

SqlCommand thisCommand = thisConnection.CreateCommand();
//给命令对象提供命令,并执行SQL操作
thisCommand.CommandText = "SELECT CustomerID,CompanyName from Customers"; SqlDataReader thisReader = thisCommand.ExecuteReader();

//while there rows to read
while(thisReader.Read())
{
//输出ID和姓名列
Console.WriteLine("\t{0}\t{1}",thisReader["CustomerID"],thisReader["CompanyName"]);
}
//close the reader
thisReader.Close(); //close the connection
thisConnection.Close();
}
}
}

解决方案 »

  1.   

    (@"Data Source = (local);Integrated Security = SSPI;" + "Initial Catalog = Northwind");
    =======================================================================
    try:
    (@"Data Source =.; uid = sa;pwd =;" + "Initial Catalog = Northwind");
    or
    (@"Data Source =127.0.0.1; uid = sa;pwd =;" + "Initial Catalog = Northwind");Integrated Security = SSPI  
    指:Windows 身份验证(通常称为集成安全性)连接到服务器数据库上