您的位置:首页 > 数据库

SQLServer 在Visual Studio的2种连接方法

2013-09-06 14:51 453 查看
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<dt.Rows.Count;i++)  {      realname=dt.Rows[i][3].ToString();      username=dt.Rows[i][1].ToString();      mobile=dt.Rows[i][4].ToString();      address=dt.Rows[i][5].ToString();      Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);  }  Console.ReadKey(); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SQLServer VisualStudio