Vs2010使用connection连接sql server2008数据库

1.配置web.conf,建立连接字符串ConnectionString

<configuration>

<appSettings>

<add key=”conStr”value=”Server=DONET2008;User Id=sa;Pwd=163.com;DataBase=master”/>

</appSettings>

………

</configuration>

其中ServerDONET2008SQLserver数据库实例名

其中………web.conf的其他配置项这里不做介绍

2.asp.cs配置文件中创建引用命名空间

using System.Data.SqlClient;

3.aspx.cs配置文件中,从配置文件中读取数据库连接字符串

string SqlStr = System.Configuration.ConfigurationManager.AppSettings["conStr"];

4.创建SqlConnection对象连接数据库

SqlConnection conn = newSqlConnection(SqlStr);

5.打开数据库的连接

conn.Open();

6.操作数据库

执行命令

7.关闭数据库

conn.Close();