|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.OleDb;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace AccessStudy.Core
|
|
|
|
|
{
|
|
|
|
|
public class OledbUtil : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public OleDbConnection DbConnection { get; set; }
|
|
|
|
|
|
|
|
|
|
public OledbUtil()
|
|
|
|
|
{
|
|
|
|
|
var accessFile = $"{AppDomain.CurrentDomain.BaseDirectory}AccessDB\\Demo.accdb";
|
|
|
|
|
var connetString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessFile}";
|
|
|
|
|
DbConnection = new OleDbConnection(connetString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OledbUtil(string connetString)
|
|
|
|
|
{
|
|
|
|
|
DbConnection = new OleDbConnection(connetString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OledbUtil(OleDbConnectionStringBuilder connectionStringBuilder)
|
|
|
|
|
{
|
|
|
|
|
DbConnection = new OleDbConnection(connectionStringBuilder.ConnectionString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表是否存在
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool HasDataTable(string dataTableName)
|
|
|
|
|
{
|
|
|
|
|
var hosTable = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
hosTable = DbConnection.GetSchema("Tables", new string[4] { null, null, dataTableName, "TABLE" }).Rows.Count > 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
hosTable = false;
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataSet
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataSet GetDataSet(string dataTableName)
|
|
|
|
|
{
|
|
|
|
|
DataSet dataSet = new DataSet();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = $"select * from {dataTableName};",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var adapter = new OleDbDataAdapter(command);
|
|
|
|
|
|
|
|
|
|
adapter.Fill(dataSet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataSet
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataSet GetDataSet(string sqlText, List<OleDbParameter> parameters = null)
|
|
|
|
|
{
|
|
|
|
|
DataSet dataSet = new DataSet();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = sqlText,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (parameters != null)
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddRange(parameters.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var adapter = new OleDbDataAdapter(command);
|
|
|
|
|
|
|
|
|
|
adapter.Fill(dataSet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataTable
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataTable GetDataTable(string dataTableName)
|
|
|
|
|
{
|
|
|
|
|
DataSet dataSet = GetDataSet(dataTableName);
|
|
|
|
|
return dataSet.Tables[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataTable
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataTable GetDataTable(string sqlText, List<OleDbParameter> parameters = null)
|
|
|
|
|
{
|
|
|
|
|
DataSet dataSet = GetDataSet(sqlText, parameters);
|
|
|
|
|
return dataSet.Tables[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataReader
|
|
|
|
|
/// 切记:用完之后主动关闭连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OleDbDataReader GetDataReader(string dataTableName)
|
|
|
|
|
{
|
|
|
|
|
OleDbDataReader dataReader = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = $"select * from {dataTableName};",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dataReader = command.ExecuteReader();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
//CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataReader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取DataReader
|
|
|
|
|
/// 切记:用完之后主动关闭连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OleDbDataReader GetDataReader(string sqlText, List<OleDbParameter> parameters = null)
|
|
|
|
|
{
|
|
|
|
|
OleDbDataReader dataReader = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = sqlText,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (parameters != null)
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddRange(parameters.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataReader = command.ExecuteReader();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
//CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataReader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取第一行第一列的值
|
|
|
|
|
/// 不存在则为null
|
|
|
|
|
/// </summary>
|
|
|
|
|
public object GetScalar(string sqlText, List<OleDbParameter> parameters = null)
|
|
|
|
|
{
|
|
|
|
|
object result = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = sqlText,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (parameters != null)
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddRange(parameters.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = command.ExecuteScalar();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行非查询语句
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>受影响行数</returns>
|
|
|
|
|
public int ExecuteNonQuery(string sqlText, List<OleDbParameter> parameters = null)
|
|
|
|
|
{
|
|
|
|
|
var result = 0;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OpenConnect();
|
|
|
|
|
var command = new OleDbCommand()
|
|
|
|
|
{
|
|
|
|
|
Connection = DbConnection,
|
|
|
|
|
CommandType = CommandType.Text,
|
|
|
|
|
CommandText = sqlText,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (parameters != null)
|
|
|
|
|
{
|
|
|
|
|
command.Parameters.AddRange(parameters.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = command.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
CloseConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开当前连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void OpenConnect()
|
|
|
|
|
{
|
|
|
|
|
if (DbConnection.State == ConnectionState.Closed)
|
|
|
|
|
{
|
|
|
|
|
DbConnection.Open();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭当前连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CloseConnect()
|
|
|
|
|
{
|
|
|
|
|
if (DbConnection.State != ConnectionState.Closed)
|
|
|
|
|
{
|
|
|
|
|
DbConnection.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (DbConnection.State == ConnectionState.Open)
|
|
|
|
|
{
|
|
|
|
|
DbConnection.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbConnection.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|