Access database accessing component (i)

Source: Internet
Author: User
Tags bool count datetime httpcontext join split trim access database
access| Access | data | Database Access database accessing component (a) 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 to 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;
}



}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.