You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
4.4 KiB
206 lines
4.4 KiB
using System;
|
|
using System.Collections;
|
|
using System.Data;
|
|
using System.Data.OleDb;
|
|
|
|
namespace Eas.Client
|
|
{
|
|
public class AccessHelper
|
|
{
|
|
/// <summary>
|
|
/// 定义连接字符串字段connstring
|
|
/// </summary>
|
|
private static string connstring;
|
|
|
|
/// <summary>
|
|
/// 定义连接字符串属性ConnString
|
|
/// </summary>
|
|
public static string ConnString
|
|
{
|
|
get
|
|
{
|
|
return connstring;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 定义连接对象字段conn
|
|
/// </summary>
|
|
private static OleDbConnection conn;
|
|
|
|
/// <summary>
|
|
/// 定义连接对象属性Conn
|
|
/// </summary>
|
|
public static OleDbConnection Conn
|
|
{
|
|
get
|
|
{
|
|
return conn;
|
|
}
|
|
set
|
|
{
|
|
conn = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Database构造函数逻辑代码用来直接初始化连接字符串ConnString
|
|
/// </summary>
|
|
public AccessHelper()
|
|
{
|
|
connstring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
|
|
//string connStr = "Provider={0};Persist Security Info=False;Data Source={1};"; //Database Password={2};
|
|
//string provider = System.Configuration.ConfigurationManager.AppSettings["Provider"].ToString();
|
|
//string path = System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["Path"].ToString();
|
|
//string password = System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
|
|
//this.connstring = string.Format(connStr, provider,path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开连接方法
|
|
/// </summary>
|
|
public static void Open()
|
|
{
|
|
if (Conn == null)
|
|
{
|
|
Conn = new OleDbConnection(ConnString);
|
|
}
|
|
if (Conn.State.Equals(ConnectionState.Closed))
|
|
{
|
|
Conn.ConnectionString = ConnString;
|
|
Conn.Open();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开连接方法
|
|
/// </summary>
|
|
public static void Open(string db)
|
|
{
|
|
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source={0}";
|
|
connStr = string.Format(connStr, db);
|
|
if (Conn == null)
|
|
{
|
|
Conn = new OleDbConnection(connStr);
|
|
}
|
|
if (Conn.State.Equals(ConnectionState.Closed))
|
|
{
|
|
Conn.ConnectionString = connStr;
|
|
Conn.Open();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭数据库连接
|
|
/// </summary>
|
|
public static void Close()
|
|
{
|
|
if (Conn != null)
|
|
{
|
|
//this.connstring = "";
|
|
Conn.Close();
|
|
Conn.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用于执行SQL语句的方法针对Update、Insert、Delete操作
|
|
/// </summary>
|
|
/// <param name="sqlString">SQL语句</param>
|
|
/// <returns>针对Update、Insert、Delete操作返回影响的行数,其他就返回-1</returns>
|
|
public static int ExecuteSql(string sqlString, string db)
|
|
{
|
|
int count = -1;
|
|
|
|
try
|
|
{
|
|
Open(db);
|
|
OleDbCommand cmd = new OleDbCommand(sqlString, Conn);
|
|
count = cmd.ExecuteNonQuery();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
count = -1;
|
|
}
|
|
finally
|
|
{
|
|
Close();
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static bool ExecuteSql(ArrayList sqlList, string db)
|
|
{
|
|
Open(db);
|
|
OleDbTransaction tran = conn.BeginTransaction();
|
|
try
|
|
{
|
|
for (int n = 0; n < sqlList.Count; n++)
|
|
{
|
|
OleDbCommand cmd = new OleDbCommand(sqlList[n].ToString());
|
|
cmd.Connection = conn;
|
|
cmd.Transaction = tran;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
tran.Commit();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
tran.Rollback();
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
conn.Close();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据指定的select语句返回一个数据集DataSet
|
|
/// </summary>
|
|
/// <param name="sqlString">SQL语句</param>
|
|
/// <returns>根据指定的select语句返回一个数据集DataSet</returns>
|
|
public static DataSet Select(string sqlString, string db)
|
|
{
|
|
try
|
|
{
|
|
Open(db);
|
|
OleDbDataAdapter sda = new OleDbDataAdapter(sqlString, conn);
|
|
DataSet ds = new DataSet();
|
|
sda.Fill(ds);
|
|
|
|
//logger.Info(sqlString);
|
|
return ds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//logger.Info(ex.Message);
|
|
|
|
//StreamWriter sw = new StreamWriter(@"c:\Log.txt");
|
|
//sw.WriteLine(ex.Message);
|
|
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
|
|
public static bool HasRow(DataSet ds)
|
|
{
|
|
return ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0;
|
|
}
|
|
|
|
public static bool HasRow(DataTable dt)
|
|
{
|
|
return dt != null && dt.Rows.Count > 0;
|
|
}
|
|
|
|
public static bool HasRow(DataRow dr)
|
|
{
|
|
return dr != null;
|
|
}
|
|
}
|
|
}
|