using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace Manage_Park
{
class AccessHelper
{
private string conn_str = null;
private OleDbConnection ole_connection = null;
private OleDbCommand ole_command = null;
private OleDbDataReader ole_reader = null;
private DataTable dt = null;
public AccessHelper()
{
conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\test\Manage_Park\Manage_Park\bin\Debug\stall.accdb";
InitDB();
}
private void InitDB()
{
ole_connection = new OleDbConnection(conn_str);
ole_command = new OleDbCommand();
}
public AccessHelper(string db_path)
{
conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + db_path + "'";
InitDB();
}
private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
{
DataTable dt_tmp = null;
DataRow dr = null;
int data_column_count = 0;
int i = 0;
data_column_count = reader.FieldCount;
dt_tmp = BuildAndInitDataTable(data_column_count);
if (dt_tmp == null)
{
return null;
}
while (reader.Read())
{
dr = dt_tmp.NewRow();
for (i = 0; i < data_column_count; ++i)
{
dr[i] = reader[i];
}
dt_tmp.Rows.Add(dr);
}
return dt_tmp;
}
private DataTable BuildAndInitDataTable(int Field_Count)
{
DataTable dt_tmp = null;
DataColumn dc = null;
int i = 0;
if (Field_Count <= 0)
{
return null;
}
dt_tmp = new DataTable();
for (i = 0; i < Field_Count; ++i)
{
dc = new DataColumn(i.ToString());
dt_tmp.Columns.Add(dc);
}
return dt_tmp;
}
public DataTable GetDataTableFromDB(string strSql)
{
if (conn_str == null)
{
return null;
}
try
{
ole_connection.Open(); //打开数据库
if (ole_connection.State == ConnectionState.Closed)
{
return null;
}
ole_command.CommandText = strSql;
ole_command.Connection = ole_connection;
ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);
dt = ConvertOleDbReaderToDataTable(ref ole_reader);
ole_reader.Close();
ole_reader.Dispose();
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return dt;
}
public int ExcuteSql(string strSql)
{
int nResult = 0;
try
{
ole_connection.Open(); //打开数据库连接
if (ole_connection.State == ConnectionState.Closed)
{
return nResult;
}
ole_command.Connection = ole_connection;
ole_command.CommandText = strSql;
nResult = ole_command.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
return nResult;
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return nResult;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace Manage_Park
{
class AccessHelper
{
private string conn_str = null;
private OleDbConnection ole_connection = null;
private OleDbCommand ole_command = null;
private OleDbDataReader ole_reader = null;
private DataTable dt = null;
public AccessHelper()
{
conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\test\Manage_Park\Manage_Park\bin\Debug\stall.accdb";
InitDB();
}
private void InitDB()
{
ole_connection = new OleDbConnection(conn_str);
ole_command = new OleDbCommand();
}
public AccessHelper(string db_path)
{
conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + db_path + "'";
InitDB();
}
private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
{
DataTable dt_tmp = null;
DataRow dr = null;
int data_column_count = 0;
int i = 0;
data_column_count = reader.FieldCount;
dt_tmp = BuildAndInitDataTable(data_column_count);
if (dt_tmp == null)
{
return null;
}
while (reader.Read())
{
dr = dt_tmp.NewRow();
for (i = 0; i < data_column_count; ++i)
{
dr[i] = reader[i];
}
dt_tmp.Rows.Add(dr);
}
return dt_tmp;
}
private DataTable BuildAndInitDataTable(int Field_Count)
{
DataTable dt_tmp = null;
DataColumn dc = null;
int i = 0;
if (Field_Count <= 0)
{
return null;
}
dt_tmp = new DataTable();
for (i = 0; i < Field_Count; ++i)
{
dc = new DataColumn(i.ToString());
dt_tmp.Columns.Add(dc);
}
return dt_tmp;
}
public DataTable GetDataTableFromDB(string strSql)
{
if (conn_str == null)
{
return null;
}
try
{
ole_connection.Open(); //打开数据库
if (ole_connection.State == ConnectionState.Closed)
{
return null;
}
ole_command.CommandText = strSql;
ole_command.Connection = ole_connection;
ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);
dt = ConvertOleDbReaderToDataTable(ref ole_reader);
ole_reader.Close();
ole_reader.Dispose();
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return dt;
}
public int ExcuteSql(string strSql)
{
int nResult = 0;
try
{
ole_connection.Open(); //打开数据库连接
if (ole_connection.State == ConnectionState.Closed)
{
return nResult;
}
ole_command.Connection = ole_connection;
ole_command.CommandText = strSql;
nResult = ole_command.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
return nResult;
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return nResult;
}
}
}
可以直接拿来用了,通用。