SQLServer 在Visual Studio的2种连接方法 一、Sql Server 在Visual Studio的连接有两种方法:(1)本地计算机连接;复制代码 代码如下:string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True"; (2)windows身份验证方式连接;复制代码 代码如下:string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码"; 二、在Visual Studio中使用:例1:查询数据库中的数据并且显示出来复制代码 代码如下:string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True"; //此处使用本地计算机连接方式 SqlConnection conn = new SqlConnection(s); //创建连接 conn.Open(); //打开连接 SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from T_User"; //使用命令 SqlDataAdapter adapter=new SqlDataAdapter(cmd); DataTable dt=new DataTable(); adapter.Fill(dt); conn.Dispose(); //释放所以资源 cmd.Dispose(); conn.Close(); //关闭连接 string realname=""; string username=""; string mobile=""; string address=""; for (int i=0;i