
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;
????}
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;
????}
?
?
?
更多文章、技術(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ì)您有幫助就好】元
