using System;
using System.Data;
using System.Data.OleDb;
namespace Bussiness.Cls
{
/// <summary>
/// UtilHelper 的摘要說明
/// </summary>
public class UtilHelper
{
public UtilHelper()
{ }
public static string ConnString = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + Application.StartupPath + "\\AnalyseFile\\DB\\DownLoad.mdb" + ";Persist Security Info=False;Jet OLEDB:Database PassWord=;";
private static string PassWord = "xyz_@#$%^9&8*4()!_+ooyq#$%";
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string sql)
{
try
{
//創建數據庫連接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//創建command對象並保存sql查詢語句
OleDbCommand command = new OleDbCommand(sql, conn);
//創建datareader 對象來連接到表單
using (OleDbDataReader reader = command.ExecuteReader())
{
return reader;
}
}
}
//一些通常的異常處理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] oledbparams)
{<br /> try
{
//創建數據庫連接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//創建command對象並保存sql查詢語句
OleDbCommand command = new OleDbCommand(sql, conn);
// 增加參數
if (oledbparams != null)
{
foreach (OleDbParameter p in oledbparams)
{
if (p != null)
{
if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null))
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
//創建datareader 對象來連接到表單
using (OleDbDataReader reader = command.ExecuteReader())
{
return reader;
}
}
}
//一些通常的異常處理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sql"></param>
public static void ExecuteNonQuery(string sql)
{
try
{
//創建數據庫連接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//創建command對象並保存sql查詢語句
OleDbCommand command = new OleDbCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
; conn.Open();
//創建datareader 對象來連接到表單
command.ExecuteNonQuery();
conn.Close();
}
}
//一些通常的異常處理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sql"></param>
public static void ExecuteNonQuery(string sql, params OleDbParameter[] oledbparams)
{
try
{
//創建數據庫連接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//創建command對象並保存sql查詢語句
// OleDbCommand command = new OleDbCommand(sql, conn);
OleDbCommand command = conn.CreateCommand();
command.CommandText = sql;
if (conn.State == ConnectionState.Closed)
conn.Open();
command.CommandType = CommandType.Text;
// 增加參數
if (oledbparams != null)
{
for (int i = 0; i < oledbparams.Length; i++)
{
command.Parameters.Add(oledbparams[i]);
}
//創建datareader 對象來連接到表單
command.ExecuteNonQuery();
conn.Close();
}
}
//一些通常的異常處理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string sql)
{
try
{
; //創建數據庫連接
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
using (OleDbDataAdapter ida = new OleDbDataAdapter(sql, conn))
{
DataSet ds = new DataSet();
ida.Fill(ds);
return ds;
}
}
}
//一些通常的異常處理
catch (OleDbException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}
}