当前位置:数据库 > SQLServer >>

网上购物系统(Task003)——通用数据库访问函数集SqlHelper类

六、代码页中的类SQLHelper

[csharp]  using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Collections.Generic; 
using WestGarden.Model; 
 
namespace WestGarden.Web 

    public partial class Default6 : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            IList<CategoryInfo> catogories = new List<CategoryInfo>(); 
 
            string connectionString = ConfigurationManager.ConnectionStrings["NetShopConnString"].ConnectionString; 
            string cmdText = "SELECT * FROM Category"; 
 
            //使用静态类,可直接调用,不需要new。  
            SqlDataReader rdr = SqlHelper.ExecuteReader(connectionString, CommandType.Text, cmdText,null); 
 
            while (rdr.Read()) 
            { 
                CategoryInfo category = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2)); 
                catogories.Add(category); 
            } 
            rdr.Close(); 
 
            repCategories.DataSource = catogories; 
            repCategories.DataBind(); 
        } 
    } 
 
    //这个类是静态的,应用程序启动时,自动驻留在内存中,所建连接,在使用过程中,  
    //也只是Close(),放回到连接池中。并没有注销Dispose(),可以下次连接时,快速使用。  
    public abstract class SqlHelper 
    { 
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) 
        { 
            SqlCommand cmd = new SqlCommand(); 
            SqlConnection conn = new SqlConnection(connectionString); 
 
            try 
            { 
                //cmd.Connection = conn;  
                //cmd.CommandType = cmdType;  
                //cmd.CommandText = cmdText;  
                 
                //conn.Open();  
 
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 
 
                //如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection,  
                //则关闭 SqlDataReader 会自动关闭此连接。  
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
                cmd.Parameters.Clear(); 
                return rdr; 
            } 
            catch 
            { 
                conn.Close(); 
                throw; 
            } 
            //finally  
            //{  
            //    conn.Close();  
            //}  
        } 
 
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) 
        { 
 
            if (conn.State != ConnectionState.Open) 
                conn.Open(); 
 
            cmd.Connection = conn; 
            cmd.CommandText = cmdText; 
 
            if (trans != null) 
                cmd.Transaction = trans; 
 
            cmd.CommandType = cmdType; 
 
            if (cmdParms != null) 
            { 
                foreach (SqlParameter parm in cmdParms) 
                    cmd.Parameters.Add(parm); 
            } 
        } 
    } 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,