zoukankan      html  css  js  c++  java
  • 网上购物系统(Task005)——通用数据库访问函数集SqlHelper类

    源代码:13033480群共享

    六、代码页中的类SQLHelper

    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);
                }
            }
        }
    }
    


    七、App_Code文件夹中的类SQLHelper.cs

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

    namespace WestGarden.Web
    {

        /// <summary>
        /// SQLHelper 的摘要说明
        /// </summary>
        //这个类是静态的,应用程序启动时,自动驻留在内存中,所建连接,在使用过程中,
        //也只是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);
                }
            }
        }
    }

    八、类库DBUtility中的类SQLHelper.cs

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;
    
    namespace WestGarden.DBUtility
    {
    
        /// <summary>
        /// SQLHelper 的摘要说明
        /// </summary>
        //这个类是静态的,应用程序启动时,自动驻留在内存中,所建连接,在使用过程中,
        //也只是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);
                }
            }
        }
    }
    


    九、制作主界面Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WestGarden.Web.Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>肯德基订餐系统——西园工作室</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Panel ID="panFocus" runat="server" DefaultButton="btnSearch">
                <table align="center" border="0" cellpadding="0" cellspacing="0" width="780">
                    <tr valign="top">
                        <td rowspan="2">
                            <img src="Images/Comm_Images/Logo_home.jpg" alt="home" /></td>
                        <td height="25" width="141">
                            <asp:TextBox ID="txtSearch" runat="server" CssClass="homeSearchBox" Width="130px"></asp:TextBox></td>
                        <td width="50">
                            <asp:ImageButton ID="btnSearch" runat="server" AlternateText="Search" CausesValidation="false"
                                CssClass="paddingSearchicon" ImageUrl="Images/Comm_Images/button-search.gif" /></td>
                        <td width="50">
                            <asp:LoginStatus ID="lgnStatus" runat="server" CssClass="homeLink" LoginText="登 录"
                                LogoutAction="Redirect" LogoutPageUrl="~/Default.aspx" LogoutText="退 出" />
                        </td>
                        <td width="64">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <img src="Images/Comm_Images/KFC.JPG" /></td>
                        <td width="64">
                        </td>
                    </tr>
                </table>
            </asp:Panel>
            <table align="center" border="0" cellpadding="0" cellspacing="0" width="780">
                <tr>
                    <td width="112">
                    </td>
                    <td bgcolor="#FFFFFF" width="5">
                    </td>
                    <td bgcolor="#FFFFFF" height="5" width="436">
                    </td>
                    <td bgcolor="#FFFFFF" style=" 5px">
                    </td>
                    <td width="224">
                    </td>
                </tr>
                <tr>
                    <td>
                         </td>
                    <td bgcolor="#FFFFFF">
                         </td>
                    <td class="welcome">
                        欢迎光临肯德基订餐网站</td>
                    <td bgcolor="#FFFFFF" style=" 5px">
                         </td>
                    <td>
                         </td>
                </tr>
                <tr>
                    <td colspan="5" height="1">
                    </td>
                </tr>
                <tr>
                    <td width="112">
                        <img height="290" src="Images/Comm_Images/door.jpg" width="112" alt="Sea Horse" /></td>
                    <td bgcolor="#FFFFFF" width="5">
                         </td>
                    <td valign="top" width="436">
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td>
                                    打开菜单,会有精彩的发现,各种美味在向您招手</td>
                            </tr>
                            <tr>
                                <td>
                                    等待着您的品尝.</td>
                            </tr>
                            <tr>
                                <td style="padding-left: 20px">
                                    <asp:Repeater ID="repCategories" runat="server">
                                        <HeaderTemplate>
                                            <table cellspacing="0" border="0" style="border-collapse: collapse;">
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <tr>
                                                <td>
                                                    <asp:HyperLink runat="server" ID="lnkCategory" NavigateUrl='<%# string.Format("~/Items.aspx?page=0&categoryId={0}", Eval("CategoryId")) %>'
                                                        Text='<%# Eval("Name") %>' /><asp:HiddenField runat="server" ID="hidCategoryId" Value='<%# Eval("CategoryId") %>' />
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            </table>
                                        </FooterTemplate>
                                    </asp:Repeater>
                                    <p>
                                         </p>
                                    <br />
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td bgcolor="#FFFFFF" valign="top" style=" 5px">
                         </td>
                    <td valign="top">
                        <img src="Images/Comm_Images/main_logo.gif" alt="西园软件出品" usemap="#mapWestGardenSoftWare"
                            border="0" /></td>
                </tr>
                <tr>
                    <td>
                         </td>
                    <td>
                         </td>
                    <td>
                        Version 1.0 版权所有:西园电脑工作室.QQ交流群:13033480</td>
                    <td style=" 5px">
                         </td>
                    <td>
                         </td>
                </tr>
            </table>
            <map id="mapWestGardenSoftWare" name="西园软件">
                <area shape="rect" coords="240,235,140,300" href="http://blog.csdn.net/yousuosi"
                    target="_blank" alt="西园电脑工作室" />
            </map>
        </form>
    </body>
    </html>
    

    版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

  • 相关阅读:
    SpringBoot-web场景-静态资源访问 & 欢迎页支持 & 自定义Favicon & 静态资源配置原理
    SpringBoot配置文件yaml文件的用法 & 自定义类绑定的配置提示
    设置 TabBarItem 选中时的图片及文字颜色
    iOS 9 使用HTTP的方法
    php单双引号嵌套解决方案
    github desktop项目版本控制
    数据库-内 | 左| 右| 全连接
    05_总结一下,以软件开发周期说明不同的测试使用
    04_用户需求 自己产品 竞争对手产品关系
    03_P52 课后作业
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138433.html
Copyright © 2011-2022 走看看