亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

SQL 工具類

系統(tǒng) 1667 0
代碼
using ?System;
using ?System.Data;
using ?System.Configuration;
using ?System.Web;
using ?System.Web.Security;
using ?System.Web.UI;
using ?System.Web.UI.HtmlControls;
using ?System.Web.UI.WebControls;
using ?System.Web.UI.WebControls.WebParts;
using ?System.Data.SqlClient;

/// ? <summary>
/// sql?Server?數(shù)據(jù)庫操作
/// ? </summary>
public ? class ?CMS_SqlHelp
{
???
private ? static ? string ?sqlconstr? = ?Convert.ToString(ConfigurationManager.ConnectionStrings[ " sqlconstr " ]);
????
public ?CMS_SqlHelp()
????{
????????
//
????????
// TODO:?在此處添加構(gòu)造函數(shù)邏輯
????????
//
???????
????}
????
/// ? <summary>
????
/// ?自定義分頁
????
/// ? </summary>
????
/// ? <param?name="tblName"> 表名 </param>
????
/// ? <param?name="strGetFields"> 需要返回的列 </param>
????
/// ? <param?name="fldName"> 排序字段名 </param>
????
/// ? <param?name="PageSize"> 每頁顯示的條數(shù) </param>
????
/// ? <param?name="PageIndex"> 頁碼 </param>
????
/// ? <param?name="doCount"> 返回記錄總數(shù),非0值則返回 </param>
????
/// ? <param?name="OrderType"> 設(shè)置排序類型,非0值則降序 </param>
????
/// ? <param?name="strWhere"> 查詢條件,不加where </param>
????
/// ? <returns> datatable </returns>
???? public ? static ?DataTable?GetData( string ?tblName,? string ?strGetFields,? string ?fldName,? int ?PageSize,? int ?PageIndex,? int ?doCount,? int ?OrderType,? string ?strWhere)
????{
????????
string ?strSQL? = ? "" ,?strTmp? = ? "" ,?strOrder? = ? "" ;
????????
if ?(doCount? != ? 0 )
????????{
????????????
if ?(strWhere? != ? "" )
????????????{
????????????????strSQL?
= ? " select?count(*)?as?Total?from? " ? + ?tblName? + ? " ?where? " ? + ?strWhere;
????????????}
????????????
else
????????????{
????????????????strSQL?
= ? " select?count(*)?as?Total?from? " ? + ?tblName;
????????????}
????????}
????????
else
????????{
????????????
if ?(OrderType? != ? 0 )
????????????{
????????????????strTmp?
= ? " <(select?min " ;
????????????????strOrder?
= ? " ?order?by? " ? + ?fldName? + ? " ?desc " ;
????????????}
????????????
else
????????????{
????????????????strTmp?
= ? " >(select?max " ;
????????????????strOrder?
= ? " ?order?by? " ? + ?fldName? + ? " ?asc " ;
????????????}
????????????
if ?(PageIndex? == ? 1 )
????????????{
????????????????
if ?(strWhere? != ? "" )
????????????????{
????????????????????strSQL?
= ? " select?top? " ? + ?PageSize? + ? " ? " ? + ?strGetFields? + ? " ?from? " ? + ?tblName? + ? " ?where? " ? + ?strWhere? + ? " ? " ? + ?strOrder;
????????????????}
????????????????
else
????????????????{
????????????????????strSQL?
= ? " select?top? " ? + ?PageSize? + ? " ? " ? + ?strGetFields? + ? " ?from? " ? + ?tblName? + ? " ? " ? + ?strOrder;
????????????????}
????????????}
????????????
else
????????????{
????????????????
if ?(strWhere? != ? "" )
????????????????{
????????????????????strSQL?
= ? " select?top? " ? + ?PageSize? + ? " ? " ? + ?strGetFields? + ? " ??from? " ? + ?tblName? + ? " ?where? " ? + ?fldName? + ? " ? " ? + ?strTmp? + ? " ( " ? + ?fldName? + ? " )?from?(select?top? " ? + ?(PageIndex? - ? 1 )? * ?PageSize? + ? " ? " ? + ?fldName? + ? " ?from? " ? + ?tblName? + ? " ?where? " ? + ?strWhere? + ? " ? " ? + ?strOrder? + ? " )?as?tblTmp)?and? " ? + ?strWhere? + ? " ? " ? + ?strOrder;
????????????????}
????????????????
else
????????????????{
????????????????????strSQL?
= ? " select?top? " ? + ?PageSize? + ? " ? " ? + ?strGetFields? + ? " ??from? " ? + ?tblName? + ? " ?where? " ? + ?fldName? + ? " ? " ? + ?strTmp? + ? " ( " ? + ?fldName? + ? " )?from?(select?top? " ? + ?(PageIndex? - ? 1 )? * ?PageSize? + ? " ? " ? + ?fldName? + ? " ?from? " ? + ?tblName? + ? "" ? + ?strOrder? + ? " )?as?tblTmp) " ? + ?strOrder;
????????????????}
????????????}
????????}
????????DataTable?dt?
= ?CMS_SqlHelp.getDataTable(strSQL);
????????
return ?dt;
????}
/// ? <summary>
/// ?執(zhí)行無返回的SQL語句
/// ? </summary>
/// ? <param?name="sqlStr"> SQL語句 </param>
/// ? <returns></returns>
???? public ? static ? bool ?ExcuteSqlServer( string ?sqlStr)
????{
????????SqlConnection?con?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?sqlcom?
= ? new ?SqlCommand();
????????sqlcom.Connection?
= ?con;
????????sqlcom.CommandText?
= ?sqlStr;
????????con.Open();
????????
try
????????{
????????????sqlcom.ExecuteNonQuery();
???????

????????????
return ? true ;
????????}
????????
catch ?(Exception?ex)
????????{
???????????
????????????errorCollecting.getError(ex);
????????????
return ? false ;
????????}
????????
finally ?{
????????????con.Close();
????????}
????}
????
#region ?ExecuteScalar
????
/// ? <summary>
????
/// ?返回所查結(jié)果第一列第一行
????
/// ? </summary>
????
/// ? <param?name="sqlStr"></param>
????
/// ? <returns></returns>
???? public ? static ? object ?ExecuteScalar( string ?sqlStr)
????{
????????SqlConnection?con?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?sqlcom?
= ? new ?SqlCommand();
????????sqlcom.Connection?
= ?con;
????????sqlcom.CommandText?
= ?sqlStr;
????????
object ?obj? = ? null ;
????????con.Open();
????????
try
????????{
?????????obj
= ??sqlcom.ExecuteScalar();
?????????
return ?obj;

???????????
????????}
????????
catch ?(Exception?ex)
????????{

????????????errorCollecting.getError(ex);
????????????
return ? false ;
????????}
????????
finally
????????{
????????????con.Close();
????????}
????}
????
#endregion
????
public ? static ?SqlDataReader?ExcuteSqlDataReader( string ?sqlStr)
????{
????????SqlConnection?con?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?sqlcom?
= ? new ?SqlCommand();
??????
????????sqlcom.Connection?
= ?con;
????????sqlcom.CommandText?
= ?sqlStr;
????????SqlDataReader?sdr?
= ? null ;
?????????????con.Open();
?????????????sdr?
= ?sqlcom.ExecuteReader(CommandBehavior.CloseConnection);
????????????
return ?sdr;
????
????????????
?????????
????}
????
/// ? <summary>
????
/// ?返回DaTable
????
/// ? </summary>
????
/// ? <param?name="sqlStr"></param>
????
/// ? <returns></returns>
???? public ? static ?DataTable?getDataTable( string ?sqlStr)
????{
????????SqlConnection?con?
= ? new ?SqlConnection(sqlconstr);
??

??????
????

????????DataTable?dt?
= ? new ?DataTable();
????????con.Open();
????????
try
????????{
????????????SqlDataAdapter?da?
= ? new ?SqlDataAdapter(sqlStr,?con);
????????????da.Fill(dt);
????????}
????????
catch ?(Exception?e)
????????{
????????????errorCollecting.getError(e);
????????}
????????
finally
????????{
????????????con.Close();
????????}
????????
return ?dt;
????}


????
#region ???ExcuteProc
????
/// ? <summary>
????
/// ?執(zhí)行無返回值Proc
????
/// ? </summary>
????
/// ? <param?name="sqlProc"></param>
???? public ? static ? void ?ExecuteProcedureNonQurey( string ?sqlProc)
????{
????????SqlConnection?con?
= ? new ?SqlConnection(sqlconstr);

????????SqlCommand?com?
= ? new ?SqlCommand();
????????com.Connection?
= ?con;
????????com.CommandText?
= ?sqlProc;
????????com.CommandType?
= ?CommandType.StoredProcedure;

????????con.Open();
????????
try
????????{
????????????com.ExecuteNonQuery();
????????????com.Dispose();

????????}
????????
catch ?(Exception?ex)
????????{
????????????errorCollecting.getError(ex);
????????}
????????
finally
????????{
????????????con.Close();

????????}
????}

????
#endregion
????
/// ? <summary>
????
/// ?執(zhí)行存儲(chǔ)過程,不返回任何值
????
/// ? </summary>
????
/// ? <param?name="storedProcedureName"> 存儲(chǔ)過程名 </param>
????
/// ? <param?name="parameters"> 參數(shù) </param>
???? /*
???????SqlParameter?sp?=?new?SqlParameter("@job_desc",?SqlDbType.VarChar,?50);
????????SqlParameter?sp2?=?new?SqlParameter("@job_id",SqlDbType.SmallInt);

????????IDataParameter[]?Idp?=?new?IDataParameter[]{sp,sp2?};
????????Idp[0].Value="adff";
????????Idp[1].Value=6;
????????CMS_sqlProc.ExecuteProcedureNonQurey("updateMy",?Idp);
???
?????
*/
????
public ? static ? void ?ExecuteProcedureNonQurey( string ?storedProcedureName,?IDataParameter[]?parameters)
????{
????????SqlConnection?connection?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?command?
= ? new ?SqlCommand(storedProcedureName,?connection);
????????command.CommandType?
= ?CommandType.StoredProcedure;
????????
if ?(parameters? != ? null )
????????{
????????????
foreach ?(SqlParameter?parameter? in ?parameters)
????????????{
????????????????command.Parameters.Add(parameter);
????????????}
????????}
????????connection.Open();
????????
try
????????{
????????????command.ExecuteNonQuery();
????????????connection.Close();
????????}
????????
catch ?(Exception?ex)
????????{
????????????errorCollecting.getError(ex);
????????}


????}

????
/// ? <summary>
????
/// ?執(zhí)行存儲(chǔ),并返回SqlDataReader
????
/// ? </summary>
????
/// ? <param?name="storedProcedureName"> 存儲(chǔ)過程名 </param>
????
/// ? <param?name="parameters"> 參數(shù) </param>
????
/// ? <returns> 包含查詢結(jié)果的SqlDataReader </returns>
???? public ? static ?SqlDataReader?ExecuteProcedureReader( string ?storedProcedureName,?IDataParameter[]?parameters)
????{
????????SqlConnection?connection?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?command?
= ? new ?SqlCommand(storedProcedureName,?connection);
????????command.CommandType?
= ?CommandType.StoredProcedure;
????????
if ?(parameters? != ? null )
????????{
????????????
foreach ?(SqlParameter?parameter? in ?parameters)
????????????{
????????????????command.Parameters.Add(parameter);
????????????}
????????}
????????connection.Open();
????????SqlDataReader?sqlReader?
= ?command.ExecuteReader(CommandBehavior.CloseConnection);
????????
return ?sqlReader;
????}

????
/// ? <summary>
????
/// ?執(zhí)行存儲(chǔ),并返回DataTable
????
/// ? </summary>
????
/// ? <param?name="storedProcedureName"> 存儲(chǔ)過程名 </param>
????
/// ? <param?name="parameters"> 參數(shù) </param>
????
/// ? <returns> 包含查詢結(jié)果的SqlDataReader </returns>
???? public ? static ?DataTable?ExecuteProcedureDataTable( string ?storedProcedureName,?IDataParameter[]?parameters)
????{
????????SqlConnection?connection?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?command?
= ? new ?SqlCommand(storedProcedureName,?connection);
????????command.CommandType?
= ?CommandType.StoredProcedure;
????????
if ?(parameters? != ? null )
????????{
????????????
foreach ?(SqlParameter?parameter? in ?parameters)
????????????{
????????????????command.Parameters.Add(parameter);
????????????}
????????}
????????connection.Open();


??????????DataTable?dt?
= ? new ?DataTable();
????????
try
????????{
????????????SqlDataAdapter?da?
= ? new ?SqlDataAdapter(command);
????????????da.Fill(dt);
????????}
????????
catch ?(Exception?e)
????????{
????????????
throw ?e;
????????}
????????
finally
????????{
????????????connection.Close();
????????}
????????
return ?dt;


????}

//可以盡量避免sqlconnection.open()操作
????
public ? static ?DataSet?ExecuteProcedureDataset( string ?storedProcedureName,?IDataParameter[]?parameters)
????{
????????SqlConnection?connection?
= ? new ?SqlConnection(sqlconstr);
????????SqlCommand?command?
= ? new ?SqlCommand(storedProcedureName,?connection);
????????command.CommandType?
= ?CommandType.StoredProcedure;
????????
if ?(parameters? != ? null )
????????{
????????????
foreach ?(SqlParameter?parameter? in ?parameters)
????????????{
????????????????command.Parameters.Add(parameter);
????????????}
????????}
????????connection.Open();

????????DataSet?ds?
= ? new ?DataSet();
????????DataTable?dt?
= ? new ?DataTable();
????????
try
????????{
????????????SqlDataAdapter?da?
= ? new ?SqlDataAdapter(command);
???????
????????????da.Fill(ds);
????????}
????????
catch ?(Exception?e)
????????{
????????????
throw ?e;
????????}
????????
finally
????????{
????????????connection.Close();
????????}
????????
return ?ds;


????}

?

?

?

SQL 工具類


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 激情爱爱视频 | 久久免费视频精品 | 免费视频久久久 | 国产精品白丝喷水在线观看 | 成在线人永久免费播放视频 | 久久这里只精品国产99热8 | 日日骚 | 午夜a一级毛片一.成 | 婷婷尹人香蕉久久天堂 | 成人在线免费观看视频 | 久久久精品久久久久久 | 99re热久久资源最新获取 | 天天操网 | 夜夜做日日做夜夜爽 | 亚洲欧美日本视频 | 狠狠干夜夜操 | 成人a毛片久久免费播放 | 一级女人18片毛片免费视频 | 日本欧美一区二区三区在线观看 | a亚洲欧美中文日韩在线v日本 | 国产免费69成人精品视频 | 国产色网| 日操夜操天天操 | 伊人久久99亚洲精品久久频 | 久久视频这里只精品3国产 久久视频这里只有精品 | 国内精品自在自线在免费 | 性欧美高清videosex | 欧美一区二区三区视频在线观看 | 伊人久久国产精品 | 国产一区二区亚洲精品 | 亚洲宗合 | 五月亭亭激情五月 | 不卡中文一二三区 | 九九天天影视 | 亚洲欧美在线综合一区二区三区 | 91视频精品 | 久久精品五月天 | 日本一区二区精品 | 亚洲国产精品综合久久久 | 性欧美日韩| 国产婷婷高清在线观看免费 |