知识大全 网购系统——通用数据库访问函数
Posted 结构
篇首语:人生必须的知识就是引人向光明方面的明灯。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 网购系统——通用数据库访问函数相关的知识,希望对你有一定的参考价值。
一 try…catch…finally结构 [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 Default : 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 ; SqlCommand cmd = new SqlCommand() SqlConnection conn = new SqlConnection(connectionString) try cmd Connection = conn; cmd CommandType = CommandType Text; cmd CommandText = cmdText; conn Open() SqlDataReader rdr = cmd ExecuteReader() while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() finally conn Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() 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 Default : 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 ; SqlCommand cmd = new SqlCommand() SqlConnection conn = new SqlConnection(connectionString) try cmd Connection = conn; cmd CommandType = CommandType Text; cmd CommandText = cmdText; conn Open() SqlDataReader rdr = cmd ExecuteReader() while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() finally conn Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() 二 using()结构 [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 Default : 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 ; SqlCommand cmd = new SqlCommand() [nextpage] //简单地说 using()结构等同于前面的try…finally结构 隐式关闭了conn using(SqlConnection conn = new SqlConnection(connectionString)) cmd Connection = conn; cmd CommandType = CommandType Text; cmd CommandText = cmdText; conn Open() SqlDataReader rdr = cmd ExecuteReader() while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() 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 Default : 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 ; SqlCommand cmd = new SqlCommand() //简单地说 using()结构等同于前面的try…finally结构 隐式关闭了conn using(SqlConnection conn = new SqlConnection(connectionString)) cmd Connection = conn; cmd CommandType = CommandType Text; cmd CommandText = cmdText; conn Open() SqlDataReader rdr = cmd ExecuteReader() while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() 三 通用的数据库访问函数 [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 Default : 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 ; SqlDataReader rdr = ExecuteReader(connectionString CommandType Text cmdText) while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText) SqlCommand cmd = new SqlCommand() SqlConnection conn = new SqlConnection(connectionString) try cmd Connection = conn; cmd CommandType = cmdType; cmd CommandText = cmdText; conn Open() [nextpage] //如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection //则关闭 SqlDataReader 会自动关闭此连接 SqlDataReader rdr = cmd ExecuteReader(CommandBehavior CloseConnection) return rdr; catch conn Close() throw; //finally // // conn Close() // 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 Default : 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 ; SqlDataReader rdr = ExecuteReader(connectionString CommandType Text cmdText) while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText) SqlCommand cmd = new SqlCommand() SqlConnection conn = new SqlConnection(connectionString) try cmd Connection = conn; cmd CommandType = cmdType; cmd CommandText = cmdText; conn Open() //如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection //则关闭 SqlDataReader 会自动关闭此连接 SqlDataReader rdr = cmd ExecuteReader(CommandBehavior CloseConnection) return rdr; catch conn Close() throw; //finally // // conn Close() // [nextpage] 四 完善一下通用数据库访问函数 [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 Default : 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 ; SqlDataReader rdr = ExecuteReader(connectionString CommandType Text cmdText null) while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText params SqlParameter[] mandParameters) 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 mandParameters) //如果创建了 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 WestGarden Model; namespace WestGarden Web public partial class Default : System Web UI Page protected void Page_Load(object sender EventArgs e) [nextpage] IList<CategoryInfo> catogories = new List<CategoryInfo>() string connectionString = ConfigurationManager ConnectionStrings[ NetShopConnString ] ConnectionString; string cmdText = SELECT * FROM Category ; SqlDataReader rdr = ExecuteReader(connectionString CommandType Text cmdText null) while (rdr Read()) CategoryInfo category = new CategoryInfo(rdr GetString( ) rdr GetString( ) rdr GetString( )) catogories Add(category) rdr Close() ddlCategories DataSource = catogories; ddlCategories DataTextField = Name ; ddlCategories DataValueField = CategoryId ; ddlCategories DataBind() public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText params SqlParameter[] mandParameters) 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 mandParameters) //如果创建了 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) [nextpage] 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) 五 使用Reapter显示分类名称 窗体代码 [] <asp:Repeater ID= repCategories runat= server > <HeaderTemplate> <table cellspacing= border= > </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:HyperLink runat= server ID= lnkCategory NavigateUrl= <%# string Format( ~/Items aspx?page= &categoryId= Eval( CategoryId )) %> Text= <%# Eval( Name ) %> /><asp:HiddenField runat= server ID= hidCategoryId Value= <%# Eval( CategoryId ) %> /> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:Repeater ID= repCategories runat= server > <HeaderTemplate> <table cellspacing= border= > </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:HyperLink runat= server ID= lnkCategory NavigateUrl= <%# string Format( ~/Items aspx?page= &categoryId= Eval( CategoryId )) %> Text= <%# Eval( Name ) %> /><asp:HiddenField runat= server ID= hidCategoryId Value= <%# Eval( CategoryId ) %> /> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> 代码页绑定代码 [csharp] repCategories DataSource = catogories; repCategories DataBind() cha138/Article/program/net/201311/13382相关参考
这就需要我们在实际开发过程中将这些数据库访问类再作一次封装经过这样的封装不仅可以达到上述的目标还可以减少操作数据库的步骤减少代码编写量在这个方面微软为我们提供了ApplicationBlock但是可惜
publicclassProtectClassTest publicstaticvoidmain(String[]args)throwsProtectClassNoAccessException
我是历尽千辛万苦拔山涉水啊终于把这个页面写完了不过这也要多感谢JSP了因为昨天心血来潮的时候突然想学JSP于是找了几篇文章看完后觉得javabean太牛X了有感觉有点象我平时写asp时的处理方式都
Oracle通用函数,分组函数,子查询 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 注意)Or
Asp常用通用函数之生成时间的整数 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 函数名GetM
PHP:汉字转为unicode的通用函数 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!<?/
usingSystem; usingSystemIO; usingSystemSecurityCryptography; namespaceCommon ///<summar
应用概述 通用数据转换工具在油气藏地质及开发工程国家重点实验室项目(PLN)油藏经营管理系统中作为其数据转换的功能模块之一运用于大港油田历史数据转换到新的油藏经营管理数据库中通用数据转换工具作为
虽然BiztalkServer提供了众多的内置功能然而在一些情况下可能还需要调用程序集中的函数因为在Net程序集中可以非常方便地调用WebService访问数据库另一方面有些函数是程序集中已经写好
希赛教育计算机专业考研专业课辅导招生 希赛教育计算机专业考研专业课辅导视频 希赛教育计算机考研专业课在线测试系统 一个递归函数的运行过程类似于多个函数的嵌套调用差别仅在于调用函数和被调用函数