web.config中数据库配置收藏
2009-02-17 12:49
260 查看
在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;
}
accesa数据库连接的设置
如果你使用Web.Config来保存数据库的连接那无疑是最好的形式,便于数据库的移植以及变动等一些不定性因素,便于维护就是了。可是,你可以在网络上找到保存SQL等一些数据库的连接,如果是ACCESS连接,就没有那样的方便了。不过,可以使用下面的措施来保证ACCESS的连接。
1.在Web.Config添加:
<appSettings>
<add key = "ConnString"
value = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source ={0}\\DataBase\\digestdb.config"/>
</appSettings>
注意,数据名字是digestdb.config(为了安全更改后缀名),放在目录DataBase下面。
2.在数据库操作的类或者在代码里面就是如下引用了:
private static string connString = String.Format( System.Configuration.ConfigurationSettings.AppSettings[ "ConnString" ],
System.Web.HttpContext.Current.Server.MapPath("."));
3.在代码中直接使用数据库连接
比如在一个单独的类里面:
private static string connString = String.Format( System.Configuration.ConfigurationSettings.AppSettings[ "ConnString" ],
System.Web.HttpContext.Current.Server.MapPath("."));
<?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;
}
accesa数据库连接的设置
如果你使用Web.Config来保存数据库的连接那无疑是最好的形式,便于数据库的移植以及变动等一些不定性因素,便于维护就是了。可是,你可以在网络上找到保存SQL等一些数据库的连接,如果是ACCESS连接,就没有那样的方便了。不过,可以使用下面的措施来保证ACCESS的连接。
1.在Web.Config添加:
<appSettings>
<add key = "ConnString"
value = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source ={0}\\DataBase\\digestdb.config"/>
</appSettings>
注意,数据名字是digestdb.config(为了安全更改后缀名),放在目录DataBase下面。
2.在数据库操作的类或者在代码里面就是如下引用了:
private static string connString = String.Format( System.Configuration.ConfigurationSettings.AppSettings[ "ConnString" ],
System.Web.HttpContext.Current.Server.MapPath("."));
3.在代码中直接使用数据库连接
比如在一个单独的类里面:
private static string connString = String.Format( System.Configuration.ConfigurationSettings.AppSettings[ "ConnString" ],
System.Web.HttpContext.Current.Server.MapPath("."));
相关文章推荐
- Web.config中关于数据库连接字符串配置问题
- asp.net数据库连接web.config配置
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- web.config中配置数据库(多数据)连接的两种方式
- web.confige 配置数据库字符串及读取
- asp.net连接数据库web.config配置
- web.config 中关于数据库配置
- web.config配置数据库连接 【转】
- ASP.NET中使用web.config配置数据库连接
- 数据库做了镜像后,Web应用中Web.config的数据库连接串配置
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- 配置数据库链接web.config---ConnectionString
- C#访问数据库使用web.config配置连接字串
- web.config中配置数据库连接的两种方式
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- web.config配置数据库连接+Ado.net对象操作sql数据库
- web.config中配置数据库连接的方式
- Web.Config文件中数据库连接配置
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法