ASP.net通过web.config设置数据库连接

 

基本例子:

<configuration>
   <!-- application specific settings -->
   <appSettings>
   <add key=ConnectionString value=server=localhost;uid=sa;pwd=;database=store />
   </appSettings>
  <configuration>
  
  public SqlDataReader GetReviews(int productID) {
  
   // 创建Connection和Command对象实例
   SqlConnection myConnection = new SqlConnection(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;
  数据库连接;return true>
本文来源【学网】网站链接是http://www.xue5.com

 oledb:

第一种方法设置:

<connectionStrings>
      <add  name = "mdbconn" [String, Required, Collection Key]   connectionString = "" [String, Required]  providerName = "System.Data.SqlClient" [String]   />
</connectionStrings>

读取:
 string strConnection = ConfigurationManager.ConnectionStrings["mdbconn"].ToString();



第二种方法设置:
<appSettings>
    <add key="strcon" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/Backup/我的文档/Visual Studio 2005/WebSites/4ec/App_Data/company.mdb"/>
  </appSettings>
 <connectionStrings/>

读取:
OleDbConnection con = new OleDbConnection(ConfigurationSettings.AppSettings["strcon"]);
 
sqlserver:

<connectionStrings>

<add name="SQLCONNECTIONSTRING01" connectionString="data Source=SIMON;database=mysimon;user id=sa;pwd=11111111" providerName="System.Data.SqlClient"></add>

<add name="SQLCONNECTIONSTRING02" connectionString="data source=SIMON; Initial Catalog=new01; Integrated Security=True" providerName="System.Data.SqlClient"></add>

</connectionStrings>
方法一SQLCONNECTIONSTRING01是通过用户“sa”来登陆SQL server数据库,访问表格mysimon

方法二SQLCONNECTIONSTRING02是通过windows验证方式登陆SQL server,不需要用户和密码,访问表格new01,应该只能是访问本机安装的SQL server!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值