ACCESS数据库访问组件(一)
2024-09-07 19:04:57
供稿:网友
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;
}
}
}
网站运营seo文章大全提供全面的站长运营经验及seo技术!