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<_转 载:http://www.qqread.com/dotnet/n221952.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- VB.NET 入门教程 (22167次浏览)
- asp.net 实现购物车详细代码 (14312次浏览)
- C#版的网站新闻发布系统 (690次浏览)
- ASP.NET2.0轻松搞定统计图表 (651次浏览)
- 使用ASP.NET AJAX实现幻灯片效果 (604次浏览)
- ASP.NET如何存取 SQLServer数据库图片 (592次浏览)
- 如何制作Asp.Net界面模板 (582次浏览)
- ASP.NET中实现DataGrid数据排序 (580次浏览)
- VB.NET读写文本文件方法 (579次浏览)
- VC#初学入门:第一个Windows程序 (536次浏览)



