国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

三層架構之數據庫訪問層完全篇(C#)

2024-07-21 02:18:05
字體:
來源:轉載
供稿:網友

using system;
using system.data;
using system.data.sqlclient;
using system.configuration;

namespace dbbase
{

public abstract class base
{

#region "fields of base calss"

protected static string strconn = configurationsettings.appsettings["strconnection"];

protected static string strsql;

#endregion


#region "properties of base class"
}

#endregion


#region "functions of base class"
public base()
{
//
// todo: add constructor logic here
//
}

/// <summary>
/// executing sql commands
/// </summary>
/// <param name="strsql">string</param>
/// <returns>return int</returns>
protected static int executesql(string strsql)
{
sqlconnection mycn = new sqlconnection(strconn);
sqlcommand mycmd = new sqlcommand(strsql,mycn);
try
{
mycn.open();
mycmd.executenonquery();
return 0;
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
finally
{
mycmd.dispose();
mycn.close();
}
}


/// <summary>
///executing sql commands
/// </summary>
/// <param name="strsql">要執(zhí)行的sql語句,為字符串類型string</param>
/// <returns>返回執(zhí)行情況,整形int</returns>
protected static int executesqlex(string strsql)
{
sqlconnection mycn = new sqlconnection(strconn);
sqlcommand mycmd = new sqlcommand(strsql,mycn);

try
{
mycn.open();
sqldatareader myreader = mycmd.executereader();
if(myreader.read())
{
return 0;
}
else
{
throw new exception("value unavailable!");
}
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
finally
{
mycmd.dispose();
mycn.close();
}
}


/// <summary>
/// get dataset
/// </summary>
/// <param name="strsql">(string)</param>
/// <returns>(dataset)</returns>
protected static dataset executesql4ds(string strsql)
{
sqlconnection mycn = new sqlconnection(strconn);
try
{
mycn.open();
sqldataadapter sda = new sqldataadapter(strsql,mycn);
dataset ds = new dataset("ds");
sda.fill(ds);
return ds;
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
finally
{
mycn.close();
}
}


/// <summary>
/// get single value
/// </summary>
/// <param name="strsql">(string)</param>
/// <returns>(int)</returns>
protected static int executesql4value(string strsql)
{
sqlconnection mycn = new sqlconnection(strconn);
sqlcommand mycmd = new sqlcommand(strsql,mycn);
try
{
mycn.open();
object r = mycmd.executescalar();
if(object.equals(r,null))
{
throw new exception("value unavailable!");
}
else
{
return (int)r;
}
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
finally
{
mycmd.dispose();
mycn.close();
}
}


/// <summary>
/// get object
/// </summary>
/// <param name="strsql">(string)</param>
/// <returns>(object)</returns>
protected static object executesql4valueex(string strsql)
{
sqlconnection mycn = new sqlconnection(strconn);
sqlcommand mycmd = new sqlcommand(strsql,mycn);
try
{
mycn.open();
object r = mycmd.executescalar();
if(object.equals(r,null))
{
throw new exception("object unavailable!");
}
else
{
return r;
}
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
finally
{
mycmd.dispose();
mycn.close();
}
}


/// <summary>
/// execute multipul sql commands
/// </summary>
/// <param name="strsqls">string</param>
/// <returns>int</returns>
protected static int executesqls(string[] strsqls)
{
sqlconnection mycn = new sqlconnection(strconn);
sqlcommand mycmd = new sqlcommand();
int j=strsqls.length;

try
{
mycn.open();
}
catch(system.data.sqlclient.sqlexception e)
{
throw new exception(e.message);
}
sqltransaction mytrans = mycn.begintransaction();

try
{
mycmd.connection = mycn;
mycmd.transaction = mytrans;

foreach(string str in strsqls)
{
mycmd.commandtext = str;
mycmd.executenonquery();
}
mytrans.commit();
return 0;
}
catch(system.data.sqlclient.sqlexception e)
{
mytrans.rollback();
throw new exception(e.message);
}
finally
{
mycmd.dispose();
mycn.close();
}
}

#endregion
}
}



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄山市| 柞水县| 宿迁市| 屯留县| 平湖市| 古蔺县| 宜黄县| 阿图什市| 万安县| 长治县| 华亭县| 东兰县| 南昌县| 莱州市| 金沙县| 汝阳县| 察雅县| 依安县| 竹溪县| 日喀则市| 屯留县| 富蕴县| 新疆| 三原县| 阜康市| 满洲里市| 通城县| 资中县| 北海市| 梨树县| 奇台县| 阳信县| 历史| 开封市| 乐业县| 舟曲县| 山东| 噶尔县| 会泽县| 崇州市| 濮阳县|