日期:2014-05-19  浏览次数:20769 次

c# 程序如何连接到SQL SERVER数据库
谁能给个例子,小弟多谢了,连接到本地数据库

------解决方案--------------------
using System;
using System.Data;
using System.Data.SqlClient;

namespace ConnectionDemo
{
class SQLServerDemo
{
[STAThread]
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection( "Server = .;Integrated Security = SSPI;DataBase=northwind ");
SqlCommand cmd = new SqlCommand( "Select CategoryID,CategoryName FROM Categories ", cn);

cn.Open();

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())
{
Console.WriteLine( "\t{0}\t{1} ", dr.GetInt32(0), dr.GetString(1));
}
dr.Close();
cn.Close();
}
}
}

------解决方案--------------------
SqlConnection objConn = new SqlConnection( "Server = 服务器; DataBase = 数据库; uid = sa; Password = 密码 ");
objConn.Open();