网站首页 > 网络编程教程 > ASP.NET教程 > 如何在web.config中建立公用的的数据库连接

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

  • 作者:互联网
  • 时间:2009-06-30 16:14:10


   
   
       
   

public SqlDataReader GetReviews(int productID) {

    // 创建Connection和Command对象实例
    SqlConnection myConnection = new SqlConnection(Co***gurationSettings.AppSettings[ConnectionString]);
    SqlCommand myCommand = new SqlCommand(ReviewsList, myConnection);

    my***mand.CommandType = Co***ndType.StoredProcedure;

    // 参数
    SqlParameter parameterProductID = new SqlParameter(@ProductID, Sq***Type.Int, 4);
    pa***eterProductID.Value = productID;
    my***mand.Parameters.Add(parameterProductID);

    // 执行
    my***nection.Open();
    SqlDataReader result = my***mand.ExecuteReader(Co***ndBehavior.CloseConnection);

    // 返回结果
    return result;
数据库连接;return true>