文章內容為本站編輯,創作.你可以任意轉載、發布、使用但請務必以明文標注文章原始出處及本聲明 http://www.opent.cn 作者:浪淘沙
此貼的方法會持續更新, 此文件要引用與數據操作的基類
using System;
using System.Data;
using System.Configuration;
using System.Web;
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>
/// EB通用與數據交互操作基類
/// </summary>
public class EBCommonObj:IDisposable
{
private bool _alreadyDispose = false;
private DBOperate dbo;
private string sql = null;
private System.Data.DataSet ds;
#region 構造與析構函數
public EBCommonObj()
{
dbo = new DBOperate();
}
~EBCommonObj()
{
dbo.Dispose();
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
dbo.Dispose();
}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 通用刪除數據庫中的某條記錄
/// <summary>
/// 通用刪除數據庫中的某條記錄
/// </summary>
/// <param name="tbl">數據表名</param>
/// <param name="fld">字段名</param>
/// <param name="IsInt">是否是int型</param>
/// <param name="kev">關鍵詞值</param>
public void CommDelByID(string tbl, string fld, bool IsInt, string key)
{
sql = "delete from {0} where {1}=";
if (IsInt)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
dbo.ExecuteNonQuery(string.Format(sql, tbl, fld, IsInt, key));
}
#endregion
#region 通用讀取數據庫中的某條記錄
/// <summary>
/// 通用讀取數據庫中的某條記錄
/// </summary>
/// <param name="tbl"></param>
/// <param name="fld"></param>
/// <param name="IsInt"></param>
/// <param name="key"></param>
/// <returns></returns>
public DataSet CommReadByID(string tbl,string fld,bool IsInt,string key)
{
sql = "select * from {0} where {1}=";
if (IsInt)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
ds = dbo.GetDataSet(string.Format(sql, tbl, fld, IsInt, key));
return ds;
}
#endregion
#region 修改數據庫中的某條記錄為true 或flase
/// <summary>
/// 修改數據庫中的某條記錄為true 或flase
/// </summary>
/// <param name="tbl">表格式</param>
/// <param name="fld">主鍵標識</param>
/// <param name="Isint">是否整形</param>
/// <param name="key">主鍵</param>
/// <param name="flgfld">flase鍵</param>
/// <param name="flgkey">key值</param>
public void CommUpdateByID(string tbl,string fld,bool Isint,string key,string flgfld,int flgkey)
{
sql = "update {0} set {4}={5} where {1}=";
if (Isint)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
dbo.ExecuteNonQuery(string.Format(sql, tbl, fld, Isint, key, flgfld, flgkey));
}
#endregion
#region 綁定DropDown 列表
/// <summary>
/// 綁定DropDown 列表
/// </summary>
/// <param name="tbl">表名</param>
/// <param name="selValue">下拉框值</param>
/// <param name="selText">下拉框顯示內容</param>
/// <param name="strWhere">where 條件語句 不用加where 沒有條件則為空</param>
/// <param name="dr">DropDownList控件名稱</param>
public void DropBind(string tbl, string selValue, string selText, string strWhere,System.Web.UI.WebControls.DropDownList dr)
{
ds = GetDrop(tbl, selValue, selText, strWhere);
dr.DataSource = ds;
dr.DataTextField = selText;
dr.DataValueField = selValue;
dr.DataBind();
ds.Clear();
ds.Dispose();
}
/// <summary>
/// 讀取表中數據
/// </summary>
/// <param name="tbl"></param>
/// <param name="selValue"></param>
/// <param name="selText"></param>
/// <param name="strWhere">條件</param>
/// <returns></returns>
public DataSet GetDrop(string tbl,string selValue,string selText,string strWhere)
{
sql = "select {1},{2} from {0} where 1=1 and {3}";
ds = dbo.GetDataSet(string.Format(sql, tbl, selValue, selText, strWhere));
return ds;
}
#endregion
#region 判斷是否有數據
/// <summary>
/// 判斷是否有數據:存在數據時返回true,否則返回Flash
/// </summary>
/// <param name="tbl">數據表名</param>
/// <param name="fld">字段名</param>
/// <param name="key">關鍵詞</param>
/// <param name="IsKeyInt">是否是數字類型:是:true;否:false</param>
/// <returns>true或false</returns>
public bool IsHaveDate(string tbl,string fld,string key,bool IsKeyInt)
{
bool Rev = false;
if (IsKeyInt)
{
sql = "select * from {0} where {1}={2}";
}
else
{
sql = "select * from {0} where {1}='{2}'";
}
ds = dbo.GetDataSet(string.Format(sql, tbl, fld, key));
if (ds.Tables[0].Rows.Count > 0)
{
Rev = true;
}
return Rev;
}
#endregion
}
}
/############################################
版權聲明:
文章內容為本站編輯,創作.你可以任意轉載、發布、使用但請務必標明文章原始出處及本聲明
http://www.opent.cn 作者:浪淘沙
############################################/
/**********************************************************************************
*
* 功能說明:數據操作基類,可以執行內聯SQL語句和存儲過程
* 作者: 劉功勳;
* 版本:V0.1(C#2.0);