关于c# 与sql server 2008数据库连接

时间:2022-04-15 05:05:09

      我用的VS2010,其它的也差不多吧。网上看到的一些两者之间的连接不是很清楚,至少对于我来说是那样。在我尝试了不下10次的时候终于被我成功了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace connectionTest
{
class Program

{
static void Main(string[] args)
{
string source = @"server=uf201002537;integrated security=SSPI;database=test1"; //可以查询了
SqlConnection conn = new SqlConnection(source );
conn.Open();
string select = "select * from student";
SqlCommand cmd = new SqlCommand(select ,conn);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
Console.WriteLine( reader[0]+" "+reader [1]);
}
conn.Close();
}
}
}


其中最主要的地方便是source ,我这uf201002537是sql server2008的服务器名字。没有如网上一些连接操作有什么" .\  "。或者  (local)\\     。并且我采用的是windows上的身份验证。没有用户名和密码。

 

 关于c# 与sql server 2008数据库连接