ACCESS数据库访问组件(一)

access|访问|数据|数据库

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;
}

}
}

时间: 2024-08-03 09:59:37

ACCESS数据库访问组件(一)的相关文章

ACCESS数据库访问组件(二)

access|访问|数据|数据库 ACCESS数据库访问组件(二)ACCESS_Table.cs using System; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// Summary description for ACCESS_DataTable. /// </summary> public class DataTable:System.Data.DataTable { pri

ACCESS数据库访问组件(三)

access|访问|数据|数据库 using System;using System.Data;using System.Data.OleDb;using System.Collections; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// Summary description for ACCESS_DataTablesCollection. /// </summary> publ

ACCESS数据库访问组件(四)

access|访问|数据|数据库 using System;using System.Data;using System.Data.OleDb;using System.Collections; namespace XLang.VideoOnline.Framework.Database.Access{ /// <summary> /// Summary description for ACCESS_DataViewsCollection. /// </summary> publi

ACCESS数据库访问的类

大部分ASP应用,都离不开对数据库的访问及操作,所以,对于数据库部分的访问操作,我们应该单独抽象出来,封装成一个单独的类.如果所用语言支持继承,可以封装一个这样的类,然后在数据操作层继承即可.下面是我写的一个ACCESS数据库访问的类,针对ACCESS作了优化,不过因为缺少足够的应用测试,可能仍然存在未知的bug及应用限制,主要代码如下: <% Class Oledb Private IDataPath Private IConnectionString Private Conn Private

SQL Artisan数据库访问组件功能概述

本文概述SQL Artisan数据库访问组件功能. SQL Artisan现有的版已经在项目中运用,在使用的过程中得到的效果相当理想.刚接触这个组件的几个新同事通过了解已有例子,很快就能适应到项目开发过程中.组件的对象操作和编译检测大提高了编写效率,在项目中得到的效果自己也有点意想不到. SQL Artisang下一个版本的功能主完善在表对象操作和对象映射方面;包括:表对象支持数据操作,对象继承,视图对象映射,统计对象映射等.为了让组件功能扩展更方便,把组件的数据映射方式进行重构,由原来的XML

Access数据库访问助手类

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Collections; namespace TaoBaoSyncLibrary.Dal { /// <summary> /// Access数据库访问助手类 /// </summary> public class DBHelper_Ac

[求助]Access数据库访问与openfiledialog

问题描述 环境:VS2010,ACCESS2010,WIN7问题:用C#开放一个软件,两个功能,一个是ACCESS数据库访问,一个是用openFileDialog对话框选取文件.在软件运行时,如果先执行openFileDialog,则没有问题,数据库访问正常,以后再执行openFileDialog也没问题,但是,如果先执行数据库访问,再执行openFileDialog时,在OpenFileDialog1.ShowDialog(this)这句就会出错,或者文件浏览对话框无响应.报错信息是:尝试读取

论数据库访问组件的选择--火地晋大作读后感

前言 火地晋做了一件有意义的事情.把这些ORM对比了一下(http://www.cnblogs.com/yelaiju/p/3209506.html). 这里要讨论一下我们用一个什么样的策略来选择数据库访问组件.通常有如下几种情况来选择: 1. 基于过去的经验    比如过去用过某某ORM,在将来的项目中继续用的话经验和熟练度就会比较高.这是建立在对该ORM的信任基础之上的. 2. 别人介绍或者在网上自己发现的,然后再试用也不错   这种情况也挺普遍的.业界同事介绍某某ORM不错,或者在网络上发

asp.net 数据库访问组件支持Using

调用代码         private void testusing()         {             using (idbhelper dbhelper = new sqlhelper(basesysteminfo.usercenterdbconnection))             {                 dbhelper.executenonquery(" select getdate() ");             }         } 源