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;
/// <summary>
/// data 的摘要說明
/// </summary>
public class data
{
public System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
public System.Data.OleDb.OleDbCommand com = new System.Data.OleDb.OleDbCommand();
public System.Data.OleDb.OleDbDataReader reader;
public System.Data.OleDb.OleDbDataAdapter ada;
public System.Data.DataSet DS=new DataSet();
public static int pagesize = 20;
public static int imagepagesize = 20;
public bool IsClose=true;//指示當前對象是否關閉
public string Error = "";
public data()//初始化連結 及 命令連結
{
Init();
}
~data()//釋放所有資源
{
Dispose();
}
public void Dispose()
{
if (!IsClose)
{
try
{
con.Close();
}
catch
{ }
if (ada != null)
ada.Dispose();
DS.Dispose();
if (reader != null)
reader.Dispose();
if (ada != null)
ada.Dispose();
com.Dispose();
con.Dispose();
IsClose = true;
}
}
public void Init()
{
if (IsClose)
{
con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["constr"];
con.Open();
com.CommandTimeout = 30;
com.Connection = con;
IsClose = false;
}
}
public bool ExeDel(string table, string id)
{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = string.Format("Delete from {0} where ID={1}",table,id);
FormatComstr();
com.ExecuteNonQuery();
return true;
}
public bool ExeCheck(string table, string id,int check)
{
try
{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = string.Format("Update {0} Set Checked={2} where id={1}", table, id, check);
FormatComstr();
com.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
public bool ExeReadOnce(string table, string FieldName,object FieldValue,bool IsNeed)
{
try
{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = string.Format("Update {0} Set ReadCount=ReadCount+1 where {1}={3}{2}{3}", table, FieldName, FieldValue, IsNeed == true ? "'" : "");
FormatComstr();
com.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
public bool ExeCom(string comstr)
{
try
{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
com.ExecuteNonQuery();
return true;
}
catch(System.Exception errors)
{
Error = errors.Message;
return false;
}
}
public System.Data.OleDb.OleDbDataReader ExeReader(string comstr)//返回閱讀器
{
//try
//{
if (reader!=null&&!reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
reader=com.ExecuteReader();
return reader;
//}
//catch
//{
//return null;
//}
}
public object ReadSingle(string table, string field, string id)//返回閱讀器
{
object re = "";
com.CommandText = string.Format("select {0} from {1} where id={2}", field, table, id);
FormatComstr();
reader = com.ExecuteReader();
if (reader.Read())
{
re = reader[field];
}
else
{
re = "";
}
reader.Close();
return re;
}
public object ReadSingle(string table, string field, string wherefield,string id)//返回閱讀器
{
// try
// {
com.CommandText = string.Format("select {0} from {1} where {3} like '{2}'", field, table, id, wherefield);
FormatComstr();
reader = com.ExecuteReader();
if (reader.Read())
{
return reader[field];
}
else
{
return null;
}
/*
}
catch
{
return null;
}*/
}
public int ExeCount(string Comstr)//返回Count
{
// try
// {
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = Comstr;
FormatComstr();
return int.Parse(com.ExecuteScalar().ToString());
/*
}
catch
{
return null;
}*/
}
public bool ListData(string comstr, System.Web.UI.WebControls.GridView DG, int page)//填充網格數據表 起始頁為 1
{
//try
//{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
ada = new System.Data.OleDb.OleDbDataAdapter(com);
DS.Clear();
ada.Fill(DS, DG.PageSize * (page - 1), pagesize, "temptable");
DG.DataSource = DS.Tables["temptable"];
DG.DataBind();
DS.Dispose();
return true;
//}
//catch
//{
//return false;
//}
}
public bool ListData(string comstr, System.Web.UI.WebControls.GridView DG, int ownpagesize,int page)//填充網格數據表 起始頁為 1
{
//try
//{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
ada = new System.Data.OleDb.OleDbDataAdapter(com);
DS.Clear();
ada.Fill(DS, DG.PageSize * (page - 1), ownpagesize, "temptable");
DG.DataSource = DS.Tables["temptable"];
DG.DataBind();
DS.Dispose();
return true;
//}
//catch
//{
//return false;
//}
}
public bool ListData(string comstr, System.Web.UI.WebControls.GridView DG)//填充網格數據表 無起始頁
{
//try
//{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
ada = new System.Data.OleDb.OleDbDataAdapter(com);
DS.Clear();
ada.Fill(DS,"temptable");
DG.DataSource = DS.Tables["temptable"];
DG.DataBind();
DS.Dispose();
return true;
// }
//catch
//{
// return false;
//}
}
public bool ListData(string comstr, System.Web.UI.WebControls.DataList DG, int page,int PageSize)//填充列表數據表 起始頁為 1
{
//try
//{
if (reader != null && !reader.IsClosed)
reader.Close();
com.CommandText = comstr;
FormatComstr();
ada = new System.Data.OleDb.OleDbDataAdapter(com);
DS.Clear();
ada.Fill(DS, PageSize * (page - 1), PageSize, "temptable");
DG.DataSource = DS.Tables["temptable"];
DG.DataBind();
DS.Dispose();
return true;
//}
//catch
//{
//return false;
//}
}
public bool isChildRange(string RangeStr)
{
return false;
}
public void FormatComstr()
{
com.CommandText.Trim();
com.CommandText = com.CommandText.Replace(" "," ").ToLower();
com.CommandText = com.CommandText.Replace("where and", "where ");
com.CommandText = com.CommandText.Replace("and and", "and");
}
}