在web.config中配置数据库连接
2005-08-10 12:19
429 查看
在asp.net应用程序下找到web.config文件,在<system.web>前面加入下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnectionString" value="server=jeff;uid=sa;pwd=btk;database=msg" />
</appSettings>
<system.web>
......
</system.web>
</configuration>
在aspx文件里面建立连接:
public SqlDataReader GetReviews(int productID) {
// 创建Connection和Command对象实例
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("ReviewsList", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
// 参数
SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.Int, 4);
parameterProductID.Value = productID;
myCommand.Parameters.Add(parameterProductID);
// 执行
myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// 返回结果
return result;
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnectionString" value="server=jeff;uid=sa;pwd=btk;database=msg" />
</appSettings>
<system.web>
......
</system.web>
</configuration>
在aspx文件里面建立连接:
public SqlDataReader GetReviews(int productID) {
// 创建Connection和Command对象实例
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("ReviewsList", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
// 参数
SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.Int, 4);
parameterProductID.Value = productID;
myCommand.Parameters.Add(parameterProductID);
// 执行
myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// 返回结果
return result;
}
相关文章推荐
- web.config配置数据库连接
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- 在Web.Config中配置数据库连接字符串
- 在web.config中配置数据库连接字符串
- web.config中配置数据库连接的两种方式
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- web.config中配置数据库连接的两种方式
- 在web.Config文件中添加数据库连接配置
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置
- web.config配置web应用程序(数据库连接)
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- webconfig.xml内配置数据库连接
- web.config中配置数据库连接的两种方式
- web.config中配置数据库连接字符串
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- ASP.NET WEB.CONFIG 配置数据库连接
- asp.net 下web.config中配置数据库连接