频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

ACCESS数据库访问组件(一)

来源: 作者: 出处:巧巧读书 2006-10-16 进入讨论组

  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 更多文章 更多内容请看Access数据库专栏数据库处理专题专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章