using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.Xml;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Collections;
namespace ACCDataProcess{
	public class UseAccData{
		string ConnectString ;//连接字符串
		public OleDbConnection Source;//使用于本次连接的Connection
		public UseAccData(string MDBFile){
			ConnectString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MDBFile;
			Source=new OleDbConnection(ConnectString);
		}
		public DataTable GetDataTable(string SQLString,string TBName,ref DataSet DT){
			try{
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DT,TBName);
				return DT.Tables[TBName];
			}
			catch{
				return null;
			}
		}
		public DataTable GetDataTable(string SQLString,string TBName){
			DataSet DT = new DataSet();
			try{
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DT,TBName);
				return DT.Tables[TBName];
			}
			catch{
				return null;
			}
		}
		public DataRow GetDataRow(string SQLString,string TBName){
			DataSet DT = new DataSet();
			try{
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DT,TBName);
				if(DT.Tables.Count>0){
					return DT.Tables[TBName].Rows[0];
				}
				else{
					return null;
				}
			}
			catch{
				return null;
			}
		}
		public int GetRecordCount(string SQLString){
			DataSet DT = new DataSet();
			try{
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DT,"test");
				return DT.Tables["test"].Rows.Count;
			}
			catch{
				return -1;
			}
		}
		public Hashtable GetAllFields(string SQLString,string TBName){
			DataSet DT = new DataSet();
			try{
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DT,TBName);
				Hashtable HT=new Hashtable();
				for(int i=0;i<=DT.Tables[TBName].Columns.Count-1;i++){
					HT.Add(DT.Tables[TBName].Columns[i].ColumnName,DT.Tables[TBName].Columns[i].ColumnName);
				}
				return HT;
			}
			catch{
				return null;
			}
		}
		public Boolean ExecAction(string SQLString){
			try{
				OleDbCommand InCmdExec = new OleDbCommand(SQLString,Source);
				InCmdExec.Connection.Open();
				InCmdExec.ExecuteNonQuery();
				InCmdExec.Connection.Close();
				return true;
			}
			catch{
				return false;
			}
		}
		public Boolean SaveToDatabase(DataTable t,string STBName){
			try{
				OleDbDataAdapter m=new OleDbDataAdapter("Select * from " + STBName,Source);
				OleDbCommandBuilder custCB = new OleDbCommandBuilder(m);
				m.Update(t);
				return true;
			}
			catch{
				return false;
			}
		}
		public string DSum(string TBName,string FDName,string Filter){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Sum(" + FDName + ") as fd From " + TBName + " where " + Filter;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return null;
			}
		}
		public string DSum(string TBName,string FDName){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Sum(" + FDName + ") as fd From " + TBName;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return null;
			}
		}
		public string Dlookup(string TBName,string FDName,string Filter){
			DataSet DS = new DataSet();
			OleDbDataAdapter m=null;
			try{
				m=new OleDbDataAdapter("select * from " + TBName + " where " + Filter,Source);
				m.Fill(DS,TBName);
				if(DS.Tables[TBName].Rows.Count>0){
					return DS.Tables[TBName].Rows[0][FDName].ToString();
				}
				else{
					return "";
				}
			}
			catch{
				return "";
			}
		}
		public string Dlookup(string TBName,string FDName){
			DataSet DS = new DataSet();
			OleDbDataAdapter m=null;
			try{
				m=new OleDbDataAdapter("select * from " + TBName,Source);
				m.Fill(DS,TBName);
				if(DS.Tables[TBName].Rows.Count>0){
					return DS.Tables[TBName].Rows[0][FDName].ToString();
				}
				else{
					return "";
				}
			}
			catch{
				return "";
			}
		}
		public string DlookupSQL(string SQLString,string FDName){
			DataSet DS = new DataSet();
			OleDbDataAdapter m=null;
			try{
				m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DS,"test");
				if(DS.Tables["test"].Rows.Count>0){
					return DS.Tables["test"].Rows[0][FDName].ToString();
				}
				else{
					return "";
				}
			}
			catch{
				return "";
			}
		}
		public int DCount(string TBName,string FDName,string Filter){
			try{
				int n=0;
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Count(" + FDName + ") as fd From " + TBName + " where " + Filter;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetInt32(0);
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return 0;
			}
		}
		public int DCount(string TBName,string FDName){
			try{
				int n=0;
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Count(" + FDName + ") as fd From " + TBName;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetInt32(0);
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return 0;
			}
		}
		public string DMax(string TBName,string FDName,string Filter){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Max(" + FDName + ") as fd From " + TBName + " where " + Filter;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return "";
			}
		}
		public string DMax(string TBName,string FDName){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Max(" + FDName + ") as fd From " + TBName;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return "";
			}
		}
		public string DMin(string TBName,string FDName,string Filter){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Min(" + FDName + ") as fd From " + TBName + " where " + Filter;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return "";
			}
		}
		public string DMin(string TBName,string FDName){
			try{
				string n="";
				OleDbCommand sm=new OleDbCommand();
				sm.Connection=Source;
				sm.CommandText="select Min(" + FDName + ") as fd From " + TBName;
				sm.CommandType = CommandType.Text;
				OleDbDataReader k;
				sm.Connection.Open();
				k=sm.ExecuteReader();
				while(k.Read()){
					n=k.GetValue(0).ToString();
				}
				sm.Connection.Close();
				return n;
			}
			catch{
				return "";
			}
		}
		public void ControlBind(string SQLString,string TBName, ref object SessionName,object SourceControl,string TypeName,Boolean tag){
			DataView dv=null;
			if(tag==true){
				DataSet DS=new DataSet();
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DS,TBName);
		    		dv = DS.Tables[TBName].DefaultView;
		    		SessionName = dv;
			}
			else{
				dv = (DataView)SessionName;
			}
			if(TypeName =="DataGrid"){
				DataGrid ctr = (DataGrid)SourceControl;
		    		ctr.DataSource = dv;
	    			ctr.DataBind();
			}
			else if(TypeName == "DataList"){
				DataList ctr = (DataList)SourceControl;
		    		ctr.DataSource = dv;
    				ctr.DataBind();
			}
	                else if(TypeName == "Repeater"){
				Repeater ctr = (Repeater)SourceControl;
		    		ctr.DataSource = dv;
	    			ctr.DataBind();
			}
	    	}
		public void DropDownListBind(string SQLString,string TBName, ref object SessionName, ref DropDownList ddL ,Boolean tag,string DataValueField,string DataTextField){
			DataView dv=null;
			if(tag==true){
				DataSet DS=new DataSet();
				OleDbDataAdapter m=new OleDbDataAdapter(SQLString,Source);
				m.Fill(DS,TBName);
		    		dv = DS.Tables[TBName].DefaultView;
				SessionName=dv;
			}
			else{
				dv = (DataView)SessionName;
			}
			ddL.DataValueField = DataValueField;
			ddL.DataTextField = DataTextField;
			ddL.DataSource = dv;
			ddL.DataBind();
		}
}
}