//摘自《visual studio2005+sql server 2005数据库应用系统开发》
//XP,visual studio2008,sql server 2005
//windows控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//添加
using System.Data;
using System.Data.SqlClient;
namespace DataSourceConn
{
class Program
{
static void Main(string[] args)
{//连接数据库的基本过程
string connectionString = GetConnectionString();
string queryString = "SELECT*FROM dbo.T_STUDENT;";//T_STUDENT已经建好的一个表
SqlConnection connection = new SqlConnection(GetConnectionString());
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", reader[0], reader[1], reader[2], reader[3]);
}
reader.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
//连接指令函数
static private string GetConnectionString()
{
return "Integrated Security=true;Initial Catalog=DB_student;Server=WWW-C749F13585D";//Server可以是计算机名。。
}
}
}