/############################################
版權(quán)聲明:
文章內(nèi)容為本站編輯,創(chuàng)作.你可以任意轉(zhuǎn)載、發(fā)布、使用但請(qǐng)務(wù)必標(biāo)明文章原始出處及本聲明
http://www.opent.cn 作者:浪淘沙
############################################/
/**********************************************************************************
*
* 功能說(shuō)明:數(shù)據(jù)操作基類(lèi),可以執(zhí)行內(nèi)聯(lián)SQL語(yǔ)句和存儲(chǔ)過(guò)程
* 作者: 劉功勛;
* 版本:V0.1(C#2.0);時(shí)間:2006-4-28
*
* *******************************************************************************/
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace EC
{
/// <summary>
/// 數(shù)據(jù)庫(kù)連接及操作對(duì)象類(lèi)
/// </summary>
public class DBBase
{
private bool _alreadyDispose = false;
private System.Data.SqlClient.SqlConnection conn;
private System.Data.SqlClient.SqlCommand com;
#region 構(gòu)造與柝構(gòu)
public DBBase()
{
try
{
conn=new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
conn.Open();
com = new System.Data.SqlClient.SqlCommand();
com.Connection = conn;
}
catch (Exception ee)
{
throw new Exception("連接數(shù)據(jù)庫(kù)出錯(cuò)");
}
}
~DBBase()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
// TODO: 此處釋放受控資源
if (com != null)
{
com.Cancel();
com.Dispose();
}
if (conn != null)
{
try
{
conn.Close();
conn.Dispose();
}
catch (Exception ee)
{
}
finally
{
conn = null;
}
}
}
// TODO: 此處釋放非受控資源。設(shè)置被處理過(guò)標(biāo)記
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 數(shù)據(jù)基本操作
/// <summary>
/// ExecuteNonQuery
/// </summary>
/// <param name="sqlString">SQL語(yǔ)句</param>
/// <returns>返回影響行數(shù)</returns>
public int ExecuteNonQuery(string sqlString)
{
int ret = 0;
com.CommandText = sqlString;
com.CommandType = CommandType.Text;
try
{
ret = com.ExecuteNonQuery();
}
catch (Exception ee)
{
throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
}
finally
{
com.Cancel();
}
return ret;
}
/// <summary>
/// 執(zhí)行插入語(yǔ)句返回IDENTITY
/// </summary>
/// <param name="sqlString">SQL語(yǔ)句</param>
/// <returns>@@IDENTITY</returns>
public int ExecInsert(string sqlString)
{
int identity = 0;
//僅能執(zhí)行Insert into 語(yǔ)句
if (!sqlString.ToLower().Contains("insert into"))
{
return -1;
}
sqlString += " Select @@IDENTITY";
System.Data.DataSet ds = new DataSet();
try
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString, conn);
da.Fill(ds);
da.Dispose();
}
catch (Exception ee)
{
throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
}
if (ds.Tables[0].Rows.Count > 0)
{
identity =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
return identity;
}
/// <summary>
/// 執(zhí)行SQL語(yǔ)句返回記錄集
/// </summary>
/// <param name="sqlString">SQL語(yǔ)句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string sqlString)
{
System.Data.DataSet ds = new DataSet();
try
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString, conn);
da.Fill(ds);
da.Dispose();
}
catch (Exception ee)
{
throw new Exception("SQL:" + sqlString + "<br />" + ee.Message.ToString());
}
return ds;
}
/// <summary>
/// 執(zhí)行存儲(chǔ)過(guò)程(返回N種參數(shù))
/// </summary>
/// <param name="procName">過(guò)程名</param>
/// <param name="hashtable">傳入的參數(shù)表</param>
/// <param name="hashtable1">傳出的參數(shù)表</param>
/// <returns>返回參數(shù)表</returns>
public System.Collections.Hashtable ExecProcedure(string procName, System.Collections.Hashtable hashtable, System.Collections.Hashtable hashtable1)
{
System.Collections.Hashtable hashtable2 = new System.Collections.Hashtable();
System.Collections.IDictionaryEnumerator ide = hashtable.GetEnumerator();
System.Collections.IDictionaryEnumerator ide1 = hashtable1.GetEnumerator();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = procName;
while (ide.MoveNext())
{
System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide.Key.ToString(), ide.Value);
com.Parameters.Add(p);
}
while (ide1.MoveNext())
{
System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide1.Key.ToString(), ide.Value);
com.Parameters.Add(p);
}
try
{
com.ExecuteNonQuery();
ide1 = hashtable1.GetEnumerator();
while (ide1.MoveNext())
{
string k = ide1.Key.ToString();
hashtable2.Add(k, com.Parameters[k].Value);
}
}
catch (Exception ee)
{
throw new Exception(ee.Message.ToString());
}
finally
{
com.Cancel();
}
return hashtable2;
}
/// <summary>
/// 執(zhí)行存儲(chǔ)過(guò)程(返回記錄集)
/// </summary>
/// <param name="procName">過(guò)程名</param>
/// <param name="hashtable">傳入的參數(shù)表</param>
/// <returns>返回記錄集</returns>
public DataSet ExecProcedure(string procName, System.Collections.Hashtable hashtable)
{
System.Data.DataSet ds = new DataSet();
com.CommandText = procName;
com.CommandType = CommandType.StoredProcedure;
System.Collections.IDictionaryEnumerator ide = hashtable.GetEnumerator();
while (ide.MoveNext())
{
System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide.Key.ToString(), ide.Value);
com.Parameters.Add(p);
}
try
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
da.Fill(ds);
da.Dispose();
}
catch (Exception ee)
{
throw new Exception(ee.Message.ToString());
}
finally
{
com.Cancel();
}
return ds;
}
#endregion
#region 數(shù)據(jù)操作
/// <summary>
/// 統(tǒng)計(jì)某表記錄總數(shù)
/// </summary>
/// <param name="KeyField">主鍵/索引鍵</param>
/// <param name="TableName">數(shù)據(jù)庫(kù).用戶(hù)名.表名</param>
/// <param name="Condition">查詢(xún)條件</param>
/// <returns>返回記錄總數(shù)</returns>
public int GetRecordCount(string keyField, string tableName, string condition)
{
int RecordCount = 0;
string sql = "select count(" + keyField + ") as count from " + tableName + " " + condition;
System.Data.DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
return RecordCount;
}
/// <summary>
/// 統(tǒng)計(jì)某表記錄總數(shù)
/// </summary>
/// <param name="Field">可重復(fù)的字段</param>
/// <param name="tableName">數(shù)據(jù)庫(kù).用戶(hù)名.表名</param>
/// <param name="condition">查詢(xún)條件</param>
/// <param name="flag">字段是否主鍵</param>
/// <returns>返回記錄總數(shù)</returns>
public int GetRecordCount(string Field, string tableName, string condition, bool flag)
{
int RecordCount = 0;
if (flag)
{
RecordCount = GetRecordCount(Field, tableName, condition);
}
else
{
string sql = "select count(distinct(" + Field + ")) as count from " + tableName + " " + condition;
System.Data.DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
}
return RecordCount;
}
/// <summary>
/// 統(tǒng)計(jì)某表分頁(yè)總數(shù)
/// </summary>
/// <param name="keyField">主鍵/索引鍵</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查詢(xún)條件</param>
/// <param name="pageSize">頁(yè)寬</param>
/// <param name="RecordCount">記錄總數(shù)</param>
/// <returns>返回分頁(yè)總數(shù)</returns>
public int GetPageCount(string keyField, string tableName, string condition, int pageSize, int RecordCount)
{
int PageCount = 0;
PageCount = (RecordCount % pageSize) > 0 ? (RecordCount / pageSize) + 1 : RecordCount / pageSize;
if (PageCount < 1) PageCount = 1;
return PageCount;
}
/// <summary>
/// 統(tǒng)計(jì)某表分頁(yè)總數(shù)
/// </summary>
/// <param name="keyField">主鍵/索引鍵</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查詢(xún)條件</param>
/// <param name="pageSize">頁(yè)寬</param>
/// <returns>返回頁(yè)面總數(shù)</returns>
public int GetPageCount(string keyField, string tableName, string condition, int pageSize, ref int RecordCount)
{
RecordCount = GetRecordCount(keyField, tableName, condition);
return GetPageCount(keyField, tableName, condition, pageSize, RecordCount);
}
/// <summary>
/// 統(tǒng)計(jì)某表分頁(yè)總數(shù)
/// </summary>
/// <param name="Field">可重復(fù)的字段</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查詢(xún)條件</param>
/// <param name="pageSize">頁(yè)寬</param>
/// <param name="flag">是否主鍵</param>
/// <returns>返回頁(yè)頁(yè)總數(shù)</returns>
public int GetPageCount(string Field, string tableName, string condition, ref int RecordCount, int pageSize, bool flag)
{
RecordCount = GetRecordCount(Field, tableName, condition, flag);
return GetPageCount(Field, tableName, condition, pageSize, ref RecordCount);
}
#endregion
#region 分頁(yè)函數(shù)
/// <summary>
/// 構(gòu)造分頁(yè)查詢(xún)SQL語(yǔ)句
/// </summary>
/// <param name="KeyField">主鍵</param>
/// <param name="FieldStr">所有需要查詢(xún)的字段(field1,field2...)</param>
/// <param name="TableName">庫(kù)名.擁有者.表名</param>
/// <param name="Condition">查詢(xún)條件1(where ...)</param>
/// <param name="Condition2">查詢(xún)條件2(order by ...)</param>
/// <param name="CurrentPage">當(dāng)前頁(yè)號(hào)</param>
/// <param name="PageSize">頁(yè)寬</param>
/// <returns>SQL語(yǔ)句</returns>
public static string JoinPageSQL(string KeyField, string FieldStr, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + " ";
}
else
{
sql = "select * from (";
sql += "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + ") a ";
sql += "where " + KeyField + " not in (";
sql += "select top " + (CurrentPage - 1) * PageSize + " " + KeyField + " from " + TableName + " " + Condition + " " + Condition2 + ")";
}
return sql;
}
/// <summary>
/// 構(gòu)造分頁(yè)查詢(xún)SQL語(yǔ)句
/// </summary>
/// <param name="Field">字段名(非主鍵)</param>
/// <param name="TableName">庫(kù)名.擁有者.表名</param>
/// <param name="Condition">查詢(xún)條件1(where ...)</param>
/// <param name="Condition2">查詢(xún)條件2(order by ...)</param>
/// <param name="CurrentPage">當(dāng)前頁(yè)號(hào)</param>
/// <param name="PageSize">頁(yè)寬</param>
/// <returns>SQL語(yǔ)句</returns>
public static string JoinPageSQL(string Field, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field;
}
else
{
sql = "select * from (";
sql += "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + " ) a ";
sql += "where " + Field + " not in (";
sql += "select top " + (CurrentPage - 1) * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + ")";
}
return sql;
}
/// <summary>
/// 頁(yè)面分頁(yè)顯示功能
/// </summary>
/// <param name="Parameters">參數(shù)串(a=1&b=2...)</param>
/// <param name="RecordCount">記錄總數(shù)</param>
/// <param name="PageSize">頁(yè)寬</param>
/// <param name="CurrentPage">當(dāng)前頁(yè)號(hào)</param>
/// <param name="ShowJump">是否顯示跳轉(zhuǎn)輸入框及按鈕</param>
/// <param name="Style">樣式(1:上頁(yè)下頁(yè)...,2:1234...)</param>
/// <returns></returns>
public static string Paging(string Parameters, int RecordCount, int PageCount, int PageSize, int CurrentPage, bool ShowJump, int Style)
{
string str;
if (RecordCount <= PageSize) return "";
if (Parameters != "") Parameters += "&";
if (CurrentPage < 1) CurrentPage = 1;
if (CurrentPage > PageCount) CurrentPage = PageCount;
str = "<table align='center' width=\"100%\"><tr><td align=\"center\">";
str += "共 " + RecordCount + " 條記錄 頁(yè)次:" + CurrentPage + "/" + PageCount + "頁(yè) ";
str += PageSize + "條/頁(yè) ";
if (Style == 1)
{
if (CurrentPage == 1)
str += "<font color=\"#999999\">首頁(yè) 上頁(yè)</font> ";
else
{
str += "<a href='?" + Parameters + "page=1' class=\"link\">首頁(yè)</a> ";
str += "<a href='?" + Parameters + "page=" + (CurrentPage - 1) + "' class=\"link\">上頁(yè)</a> "; ;
}
if (CurrentPage == PageCount )
{
str += "<font color=\"#999999\">下頁(yè) 尾頁(yè)</font> ";
}
else
{
str += "<a href='?" + Parameters + "page=" + (CurrentPage + 1) + "' class=\"link\">下頁(yè)</a> ";
str += "<a href='?" + Parameters + "page=" + PageCount + "' class=\"link\">尾頁(yè)</a> ";
}
}
else if (Style == 2)
{
int NumberSize = 10;
int PageNumber = (CurrentPage - 1) / NumberSize;
if (PageNumber * NumberSize > 0)
str += "<a href='?" + Parameters + "page=" + PageNumber * NumberSize + "' title=上十頁(yè) >[<<]</a> ";
int i;
for (i = PageNumber * NumberSize + 1; i <= (PageNumber + 1) * NumberSize; i++)
{
if (i == CurrentPage)
str += "<strong><font color=#ff0000>[" + i + "]</font></strong> ";
else
str += "<a href='?" + Parameters + "page=" + i + "'>[" + i + "]</a> ";
if (i == PageCount) break;
}
if (i < RecordCount) str += "<a href='?" + Parameters + "page=" + i + "' title=下十頁(yè)>[>>]</a> ";
}
if (ShowJump)
{
str += "";
}
str += "</td></tr></table>";
return str;
}
#endregion
}
}
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com