ADO.NET新手问题
最近转入ADO.NET学习
第一个例子运行就出错了,
高手帮忙看下看,
不明白,抄的例子,就是运行出错,
using System;
using System.Data ;
using System.Data .SqlClient ;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string connectionString =@ "data source=localhost;Initial catalog=Northwind;integrated security=SSPI ";
string commandString=@ "SELECT *FROM Customers ";
SqlDataAdapter dataAdapter =new SqlDataAdapter (commandString ,connectionString);
DataSet myDataSet=new DataSet ();
dataAdapter.Fill (myDataSet); //运行到这句时出错
DataTable table =myDataSet.Tables [0];
int numberRows=table.Rows .Count ;
}
}
}
//
未处理的“
System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
------解决方案--------------------连接串的问题吧
data source=localhost;Initial catalog=Northwind;integrated security=SSPI
改为
data source=local;Initial catalog=Northwind;integrated security=SSPI
或
data source=.;Initial catalog=Northwind;integrated security=SSPI
再试试看!
------解决方案--------------------晕,做一下,没错呀!
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connectionString = @ "data source=localhost;Initial catalog=Northwind;integrated security=SSPI ";
string commandString = @ "SELECT * FROM Customers ";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString);
DataSet myDataSet = new DataSet();
dataAdapter.Fill(myDataSet); // 没错呀!
DataTable table = myDataSet.Tables[0];
int numberRows = table.Rows.Count; // 91
}
}
}