ab56a9bcf7
SVN-Revision: r240
335 lines
9.9 KiB
C#
335 lines
9.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Data.OleDb;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Lskj_SyncAccessData
|
|
{
|
|
public class AccessDBConfig
|
|
{
|
|
// 连接数据源
|
|
private OleDbConnection conn = null;
|
|
|
|
private static string connStr = string.Empty;
|
|
|
|
private static AccessDBConfig _instance = null;
|
|
|
|
/// <summary>
|
|
/// DBConfig静态单例对象
|
|
/// </summary>
|
|
/// <value>The instance.</value>
|
|
public static AccessDBConfig Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new AccessDBConfig();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:创建数据库连接</para>
|
|
/// <para>创建人:钱雄</para>
|
|
/// <para>创建日期</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
public OleDbConnection CreateConnection(string conStr)
|
|
{
|
|
connStr = conStr;
|
|
if (conn != null)
|
|
{
|
|
try
|
|
{
|
|
conn.Close();
|
|
conn.Dispose();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
conn = null;
|
|
}
|
|
try
|
|
{
|
|
conn = new OleDbConnection(connStr);
|
|
if (conn.State == ConnectionState.Closed)
|
|
{
|
|
conn.Open();
|
|
}
|
|
return conn;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:查询Access数据库表中是否包含某个字段</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
public bool checkField(String sTblName, String sFldName)
|
|
{
|
|
bool isExist = false;
|
|
try
|
|
{
|
|
//OleDbConnection aConnection = new OleDbConnection(DB.getConnectStr());
|
|
//aConnection.Open();
|
|
|
|
object[] oa = { null, null, sTblName, sFldName };
|
|
|
|
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, oa);
|
|
if (schemaTable.Rows.Count == 0)
|
|
{
|
|
isExist = false;
|
|
}
|
|
else
|
|
{
|
|
isExist = true;
|
|
}
|
|
//MessageBox.Show((schemaTable.Rows.Count == 0 ? "不存在" : "存在"));
|
|
//aConnection.Close();
|
|
}
|
|
catch (Exception err)
|
|
{
|
|
MessageBox.Show(err.Message);
|
|
}
|
|
return isExist;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 执行SQL语句,返回影响的记录数
|
|
/// </summary>
|
|
/// <param name="SQLString">SQL语句</param>
|
|
/// <returns>影响的记录数</returns>
|
|
public int ExecuteSql(string SQLString)
|
|
{
|
|
using (OleDbConnection connection = new OleDbConnection(connStr))
|
|
{
|
|
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
int rows = cmd.ExecuteNonQuery();
|
|
return rows;
|
|
}
|
|
catch (System.Data.OleDb.OleDbException E)
|
|
{
|
|
connection.Close();
|
|
throw new Exception(E.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行返回多个查询时使用,返回DataTable类型
|
|
/// </summary>
|
|
/// <param name="sql">Sql命令</param>
|
|
/// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
|
|
/// <param name="pms">参数</param>
|
|
/// <returns></returns>
|
|
public DataTable ExecuteDataTable(string sql)
|
|
{
|
|
DataTable dt = new DataTable();
|
|
try
|
|
{
|
|
//通过adapter读取数据。
|
|
using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connStr))
|
|
{
|
|
adapter.Fill(dt);
|
|
return dt;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
|
|
/// </summary>
|
|
/// <param name="sql">查询语句</param>
|
|
/// <returns>DataSet</returns>
|
|
public DataSet GetDataSet(string sql)
|
|
{
|
|
DataSet ds = new DataSet();
|
|
try
|
|
{
|
|
this.CreateConnection(connStr);
|
|
OleDbCommand cmd = new OleDbCommand(sql, conn);
|
|
cmd.CommandTimeout = 20;
|
|
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
|
|
adapter.Fill(ds, "tempTable");
|
|
conn.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ds = null;
|
|
}
|
|
return ds;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
|
|
/// </summary>
|
|
/// <param name="sql">查询语句</param>
|
|
/// <param name="sRecord">开始记录数</param>
|
|
/// <param name="mRecord">最大记录数</param>
|
|
/// <returns>DataSet</returns>
|
|
public DataSet GetDataSet(string sql, int sRecord, int mRecord)
|
|
{
|
|
DataSet ds = new DataSet();
|
|
try
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(sql, conn);
|
|
cmd.CommandTimeout = 20;
|
|
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
|
|
adapter.Fill(ds, sRecord, mRecord, "tempTable");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ds = null;
|
|
}
|
|
return ds;
|
|
}
|
|
/// <summary>
|
|
/// 对数据库的增,删,改的操作
|
|
/// </summary>
|
|
/// <param name="sql">SQL语句</param>
|
|
/// <returns>是否成功</returns>
|
|
public bool ExecuteDataBase(string sql)
|
|
{
|
|
bool succeed = false;
|
|
int cnt = 0;
|
|
try
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(sql, conn);
|
|
cmd.CommandTimeout = 20;
|
|
if (this.Open())
|
|
cnt = cmd.ExecuteNonQuery();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
e.ToString();
|
|
}
|
|
finally
|
|
{
|
|
if (cnt > 0)
|
|
{
|
|
succeed = true;
|
|
}
|
|
}
|
|
return succeed;
|
|
}
|
|
/// <summary>
|
|
/// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
|
|
/// </summary>
|
|
/// <param name="sql">查询语句</param>
|
|
/// <returns>返回的第一行第一列的值</returns>
|
|
public string GetScalar(string sql)
|
|
{
|
|
string str = null;
|
|
try
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(sql, conn);
|
|
if (this.Open())
|
|
str = cmd.ExecuteScalar().ToString();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
e.ToString();
|
|
}
|
|
return str;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得该SQL查询返回DataTable,如果没有查询到则返回NULL
|
|
/// </summary>
|
|
/// <param name="sql">查询语句</param>
|
|
/// <returns></returns>
|
|
public DataTable GetDataTable(string sql)
|
|
{
|
|
DataTable tb = null;
|
|
DataSet ds = this.GetDataSet(sql);
|
|
if (ds != null)
|
|
{
|
|
tb = ds.Tables["tempTable"];
|
|
}
|
|
return tb;
|
|
}
|
|
/// <summary>
|
|
/// 打开数据库连接.
|
|
/// </summary>
|
|
private bool Open()
|
|
{
|
|
bool succeed = false;
|
|
try
|
|
{
|
|
if (conn.State == System.Data.ConnectionState.Closed)
|
|
{
|
|
conn.Open();
|
|
succeed = true;
|
|
}
|
|
else if (conn.State == System.Data.ConnectionState.Broken)
|
|
{
|
|
conn.Close();
|
|
conn.Open();
|
|
succeed = true;
|
|
}
|
|
else if (conn.State == System.Data.ConnectionState.Open)
|
|
{
|
|
succeed = true;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
e.ToString();
|
|
}
|
|
|
|
return succeed;
|
|
}
|
|
/// <summary>
|
|
/// 关闭数据库连接
|
|
/// </summary>
|
|
public void Close()
|
|
{
|
|
if (conn != null)
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 释放数据库连接资源
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (conn != null)
|
|
{
|
|
conn.Dispose();
|
|
conn = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|