c#连接 sql Server 2000
服务器:PC-201412270951
测试数据库:test
测试表:stu
用的工具是Mircosoft visual c# 2010 学习版
到 connection.Open();//不能执行这一步
//////////////////代码如下///////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace SQL_server连接测试l
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1");
//连接到数据库
string conString = "Server=." +
"data source=(local);" +
"Database=test;"+
"User ID=sa;"+
"Password=sa";
Console.WriteLine("2");
SqlConnection connection = new SqlConnection(conString);
SqlDataReader dataReader = null;
try{
Console.WriteLine("3");
connection.Open();//不能执行这一步
Console.WriteLine("Got connection");
//给一个command 对象传递一个连接
SqlCommand cmd = new SqlCommand("select * from stu",connection);
//获取查询记录
dataReader = cmd.ExecuteReader();
//打印每条记录的姓名
while(dataReader.Read()){
Console.WriteLine(dataReader[0]);
}
}catch(Exception e){
Console.WriteLine(e.Message);
}finally{
if(dataReader != null){
dataReader.Close();
}
if(connection != null){
connection.Close();
}
Console.WriteLine("ok");
}
Console.ReadKey();
}
}
}
服务器:PC-201412270951
测试数据库:test
测试表:stu
用的工具是Mircosoft visual c# 2010 学习版
到 connection.Open();//不能执行这一步
//////////////////代码如下///////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace SQL_server连接测试l
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1");
//连接到数据库
string conString = "Server=." +
"data source=(local);" +
"Database=test;"+
"User ID=sa;"+
"Password=sa";
Console.WriteLine("2");
SqlConnection connection = new SqlConnection(conString);
SqlDataReader dataReader = null;
try{
Console.WriteLine("3");
connection.Open();//不能执行这一步
Console.WriteLine("Got connection");
//给一个command 对象传递一个连接
SqlCommand cmd = new SqlCommand("select * from stu",connection);
//获取查询记录
dataReader = cmd.ExecuteReader();
//打印每条记录的姓名
while(dataReader.Read()){
Console.WriteLine(dataReader[0]);
}
}catch(Exception e){
Console.WriteLine(e.Message);
}finally{
if(dataReader != null){
dataReader.Close();
}
if(connection != null){
connection.Close();
}
Console.WriteLine("ok");
}
Console.ReadKey();
}
}
}
