分享
 
 
 

ACCESS数据库访问组件(一)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

ACCESS数据库访问组件(一)ACCESS_Database.cs

using System;

using System.Data;

using System.Data.OleDb;

using System.Collections;

namespace XLang.VideoOnline.Framework.Database.Access

{

/// <summary>

/// XLang.VideoOnline.Framework.Database is designed for create, update, insert and delete operations.

/// </summary>

public class Access

{

private OleDbConnection _connection;

private string _connectionString;

private string _databaseName;

private Database.Access.DataTablesCollection _tables;

private Database.Access.DataViewsCollection _views;

private DateTime _creationTime;

private DateTime _lastAccessTime;

private DateTime _lastWriteTime;

public string Name

{

get

{

return _databaseName;

}

}

public Database.Access.DataTablesCollection Tables

{

get

{

return _tables;

}

}

public Database.Access.DataViewsCollection Views

{

get

{

return _views;

}

}

public DateTime CreationTime

{

get

{

return _creationTime;

}

}

public DateTime LastAccessTime

{

get

{

return _lastAccessTime;

}

}

public DateTime LastWriteTime

{

get

{

return _lastWriteTime;

}

}

public Access()

{

}

public Access(string databaseName)

{

string delimStr = " :\\./";

char [] delimiter = delimStr.ToCharArray();

string [] split = null;

split=databaseName.Split(delimiter);

_databaseName = split[split.Length-2];

_connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+databaseName;

_creationTime = System.IO.File.GetCreationTime(databaseName);

_lastAccessTime = System.IO.File.GetLastAccessTime(databaseName);

_lastWriteTime = System.IO.File.GetLastWriteTime(databaseName);

_connection = new OleDbConnection( _connectionString );

try

{

if(!(_connection.State==ConnectionState.Open)) _connection.Open();

_tables=new Database.Access.DataTablesCollection(_connection);

_views=new DataViewsCollection(_connection);

}

catch(Exception e)

{

System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_Access:</font>"+e.Message+"<br>");

}

finally

{

if(_connection.State==ConnectionState.Open) _connection.Close();

}

}

public bool ExecuteCommand(string commandString,CommandType commandType)

{

switch(commandType)

{

case CommandType.Create:

return Create(commandString);

case CommandType.Delete:

return Delete(commandString);

case CommandType.Insert:

return Insert(commandString);

case CommandType.Select:

return Select(commandString);

case CommandType.Join:

return Join(commandString);

case CommandType.Update:

return Update(commandString);

case CommandType.View:

return View(commandString);

case CommandType.Other:

return Other(commandString);

default:

break;

}

return true;

}

private bool Create(string commandString)

{

return CreateDeleteInsertUpdate(commandString);

}

private bool Delete(string commandString)

{

return CreateDeleteInsertUpdate(commandString);

}

private bool Insert(string commandString)

{

return CreateDeleteInsertUpdate(commandString);

}

private bool Select(string commandString)

{

try

{

OleDbDataAdapter adapter=new OleDbDataAdapter(commandString,_connection);

// string tableName=null;

// string delimStr = " ";

// char [] delimiter = delimStr.ToCharArray();

// string [] split = null;

// split=commandString.Split(delimiter);

// tableName=split[4].Trim();

string from="FROM";

int fromIndex=commandString.IndexOf(from);

string subCommandString=commandString.Substring(fromIndex+4).Trim();

int endIndex=subCommandString.IndexOf(' ');

string tableName2=null;

if(endIndex<0)

tableName2=subCommandString.Substring(0).Trim();

else

tableName2=subCommandString.Substring(0,endIndex).Trim();

//System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_Select:</font>"+tableName2+"<br>");

_tables[TableNameToIndex(tableName2)].Clear();

adapter.Fill(_tables[TableNameToIndex(tableName2)]);

adapter=null;

}

catch(Exception e)

{

System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_Select:</font>"+e.Message+"<br>");

return false;

}

finally

{

}

return true;

}

private bool Join(string commandString)

{

try

{

OleDbDataAdapter adapter=new OleDbDataAdapter(commandString,_connection);

// string tableName=null;

// string delimStr = " ";

// char [] delimiter = delimStr.ToCharArray();

// string [] split = null;

// split=commandString.Split(delimiter);

// tableName=split[4].Trim();

// string from="FROM";

// int fromIndex=commandString.IndexOf(from);

// string subCommandString=commandString.Substring(fromIndex+4).Trim();

// int endIndex=subCommandString.IndexOf(' ');

// string tableName2=null;

// if(endIndex<0)

// tableName2=subCommandString.Substring(0).Trim();

// else

// tableName2=subCommandString.Substring(0,endIndex).Trim();

//System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_Select:</font>"+tableName2+"<br>");

// _tables[TableNameToIndex(tableName2)].Clear();

// adapter.Fill(_tables[TableNameToIndex(tableName2)]);

// if(_tables["temp"]!=null)

// _tables[TableNameToIndex("temp")].Clear();

// else

// {

// _tables[_tables.Count]=new Database.Access.DataTable("temp");

// _tables[TableNameToIndex("temp")].Clear();

// }

_tables[TableNameToIndex("temp")].Clear();

adapter.Fill(_tables[TableNameToIndex("temp")]);

adapter=null;

}

catch(Exception e)

{

System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_Join:</font>"+e.Message+"<br>");

return false;

}

finally

{

}

return true;

}

private int TableNameToIndex(string tableName)

{

for(int i=0;i<_tables.Count;i++)

{

if(_tables[i].Name.ToUpper()==tableName.ToUpper())

return i;

}

return -1;

}

private int ViewNameToIndex(string viewName)

{

for(int i=0;i<_tables.Count;i++)

{

if(_views[i].Name.ToUpper()==viewName.ToUpper())

return i;

}

return -1;

}

private bool Update(string commandString)

{

return CreateDeleteInsertUpdate(commandString);

}

private bool CreateDeleteInsertUpdate(string commandString)

{

if(!(_connection.State==ConnectionState.Open)) _connection.Open();

// Start a local transaction.

OleDbTransaction myTrans =_connection.BeginTransaction();

// Enlist the command in the current transaction.

OleDbCommand myCommand = _connection.CreateCommand();

myCommand.Transaction = myTrans;

try

{

myCommand.CommandText = commandString;

myCommand.ExecuteNonQuery();

myTrans.Commit();

}

catch(Exception e)

{

try

{

myTrans.Rollback();

}

catch (OleDbException ex)

{

if (myTrans.Connection != null)

{

//Console.WriteLine("An exception of type " + ex.GetType() +

// " was encountered while attempting to roll back the transaction.");

}

}

return false;

}

finally

{

if(_connection.State==ConnectionState.Open) _connection.Close();

}

return true;

}

private bool View(string commandString)

{

try

{

string from="FROM";

int fromIndex=commandString.IndexOf(from);

string subCommandString=commandString.Substring(fromIndex+4).Trim();

int endIndex=subCommandString.IndexOf(' ');

string viewName=null;

if(endIndex<0)

viewName=subCommandString.Substring(0).Trim();

else

viewName=subCommandString.Substring(0,endIndex).Trim();

OleDbDataAdapter adapter=new OleDbDataAdapter(commandString,_connection);

_views[ViewNameToIndex(viewName)].Clear();

adapter.Fill(_views[ViewNameToIndex(viewName)]);

adapter=null;

}

catch(Exception e)

{

System.Web.HttpContext.Current.Response.Write("<br><font color=#ff0000>ACCESS_Database_View:</font>"+e.Message+"<br>");

return false;

}

finally

{

}

return true;

}

private bool Other(string commandString)

{

return true;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有