数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

网购系统——通用数据库访问函数


发布日期:2023年09月03日
 
网购系统——通用数据库访问函数
try…catch…finally结构

[csharp] using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

cmdConnection = conn;

cmdCommandType = CommandTypeText;

cmdCommandText = cmdText;

connOpen()

SqlDataReader rdr = cmdExecuteReader()

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

}

finally

{

connClose()

}

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

}

}

using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

cmdConnection = conn;

cmdCommandType = CommandTypeText;

cmdCommandText = cmdText;

connOpen()

SqlDataReader rdr = cmdExecuteReader()

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

}

finally

{

connClose()

}

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

}

}

using()结构

[csharp] using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlCommand cmd = new SqlCommand()[nextpage]

//简单地说using()结构等同于前面的try…finally结构隐式关闭了conn

using(SqlConnection conn = new SqlConnection(connectionString))

{

cmdConnection = conn;

cmdCommandType = CommandTypeText;

cmdCommandText = cmdText;

connOpen()

SqlDataReader rdr = cmdExecuteReader()

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

}

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

}

}

using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlCommand cmd = new SqlCommand()

//简单地说using()结构等同于前面的try…finally结构隐式关闭了conn

using(SqlConnection conn = new SqlConnection(connectionString))

{

cmdConnection = conn;

cmdCommandType = CommandTypeText;

cmdCommandText = cmdText;

connOpen()

SqlDataReader rdr = cmdExecuteReader()

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

}

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

}

}

通用的数据库访问函数

[csharp] using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlDataReader rdr = ExecuteReader(connectionString CommandTypeText cmdText)

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText)

{

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

cmdConnection = conn;

cmdCommandType = cmdType;

cmdCommandText = cmdText;

connOpen()

[nextpage]

//如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection

//则关闭 SqlDataReader 会自动关闭此连接

SqlDataReader rdr = cmdExecuteReader(CommandBehaviorCloseConnection)

return rdr;

}

catch

{

connClose()

throw;

}

//finally

//{

// connClose()

//}

}

}

}

using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlDataReader rdr = ExecuteReader(connectionString CommandTypeText cmdText)

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText)

{

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

cmdConnection = conn;

cmdCommandType = cmdType;

cmdCommandText = cmdText;

connOpen()

//如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection

//则关闭 SqlDataReader 会自动关闭此连接

SqlDataReader rdr = cmdExecuteReader(CommandBehaviorCloseConnection)

return rdr;

}

catch

{

connClose()

throw;

}

//finally

//{

// connClose()

//}

}

}

}

[nextpage]

完善一下通用数据库访问函数

[csharp] using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlDataReader rdr = ExecuteReader(connectionString CommandTypeText cmdTextnull)

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText params SqlParameter[] commandParameters)

{

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

//cmdConnection = conn;

//cmdCommandType = cmdType;

//cmdCommandText = cmdText;

//connOpen()

PrepareCommand(cmd conn null cmdType cmdText commandParameters)

//如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection

//则关闭 SqlDataReader 会自动关闭此连接

SqlDataReader rdr = cmdExecuteReader(CommandBehaviorCloseConnection)

cmdParametersClear()

return rdr;

}

catch

{

connClose()

throw;

}

//finally

//{

// connClose()

//}

}

private static void PrepareCommand(SqlCommand cmd SqlConnection conn SqlTransaction trans CommandType cmdType string cmdText SqlParameter[] cmdParms)

{

if (connState != ConnectionStateOpen)

connOpen()

cmdConnection = conn;

cmdCommandText = cmdText;

if (trans != null)

cmdTransaction = trans;

cmdCommandType = cmdType;

if (cmdParms != null)

{

foreach (SqlParameter parm in cmdParms)

cmdParametersAdd(parm)

}

}

}

}

using System;

using SystemData;

using SystemDataSqlClient;

using SystemConfiguration;

using SystemCollectionsGeneric;

using WestGardenModel;

namespace WestGardenWeb

{

public partial class Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

[nextpage]

IList<CategoryInfo> catogories = new List<CategoryInfo>()

string connectionString = ConfigurationManagerConnectionStrings[NetShopConnString]ConnectionString;

string cmdText = SELECT * FROM Category;

SqlDataReader rdr = ExecuteReader(connectionString CommandTypeText cmdTextnull)

while (rdrRead())

{

CategoryInfo category = new CategoryInfo(rdrGetString( rdrGetString( rdrGetString())

catogoriesAdd(category)

}

rdrClose()

ddlCategoriesDataSource = catogories;

ddlCategoriesDataTextField = Name;

ddlCategoriesDataValueField = CategoryId;

ddlCategoriesDataBind()

}

public static SqlDataReader ExecuteReader(string connectionString CommandType cmdType string cmdText params SqlParameter[] commandParameters)

{

SqlCommand cmd = new SqlCommand()

SqlConnection conn = new SqlConnection(connectionString)

try

{

//cmdConnection = conn;

//cmdCommandType = cmdType;

//cmdCommandText = cmdText;

//connOpen()

PrepareCommand(cmd conn null cmdType cmdText commandParameters)

//如果创建了 SqlDataReader 并将 CommandBehavior 设置为 CloseConnection

//则关闭 SqlDataReader 会自动关闭此连接

SqlDataReader rdr = cmdExecuteReader(CommandBehaviorCloseConnection)

cmdParametersClear()

return rdr;

}

catch

{

connClose()

throw;

}

//finally

//{

// connClose()

//}

}

private static void PrepareCommand(SqlCommand cmd SqlConnection conn SqlTransaction trans CommandType cmdType string cmdText SqlParameter[] cmdParms)

{

[nextpage]

if (connState != ConnectionStateOpen)

connOpen()

cmdConnection = conn;

cmdCommandText = cmdText;

if (trans != null)

cmdTransaction = trans;

cmdCommandType = cmdType;

if (cmdParms != null)

{

foreach (SqlParameter parm in cmdParms)

cmdParametersAdd(parm)

}

}

}

}

使用Reapter显示分类名称

窗体代码

[html] <asp:Repeater ID=repCategories runat=server>

<HeaderTemplate>

<table cellspacing= border= >

</HeaderTemplate>

<ItemTemplate>

<tr>

<td>

<asp:HyperLink runat=server ID=lnkCategory NavigateUrl=<%# stringFormat(~/Itemsaspx?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=<%# stringFormat(~/Itemsaspx?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] repCategoriesDataSource = catogories;

repCategoriesDataBind()               

上一篇:动态的SQL分页

下一篇:数据库连接池Data Connection Framework