当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  如何在web.config中建立公用的的数据库连接

 如何在web.config中建立公用的的数据库连接

点击次数:23 次 发布日期:2008-11-26 12:45:40 作者:源代码网
源代码网推荐      <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>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华