Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET入门教程

SQLServer 在Visual Studio的2种连接方法

来源:中文源码网    浏览:194 次    日期:2024-05-04 03:43:27
【下载文档:  SQLServer 在Visual Studio的2种连接方法.txt 】


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

相关内容