using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.Net;
using System.IO;
namespace 抽籤選號
{
public class DataAccess
{
public DataAccess()
{
}
public static bool DataIsChange;
#region 設定資料庫連接字串
/// <summary>
/// 設定資料庫連接字串
/// </summary>
///
private static string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db\\db.mdb;Persist Security Info=False;";
#endregion
public DataAccess(string path)
{
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "\\db\\db.mdb;Persist Security Info=False;";
}
#region 執行SQL語句,返回Bool值
/// <summary>
/// 執行SQL語句,返回Bool值
/// </summary>
/// <param name="sql">要執行的SQL語句</param>
/// <returns>返回BOOL值,True為執行成功</returns>
public bool ExecuteSQL(string sql)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}
}
#endregion
#region 執行SQL語句,返回DataTable
/// <summary>
/// 執行SQL語句,返回DataTable
/// </summary>
/// <param name="sql">要執行的SQL語句</param>
/// <returns>返回DataTable類型的執行結果</returns>
public DataSet GetDataSet(string sql)
{
// System.Windows.Forms.MessageBox.Show(ConnectionString);
DataSet ds = new DataSet();
OleDbConnection con = new OleDbConnection(ConnectionString);
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
try
{
da.Fill(ds, "tb");
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
return ds;
}
#endregion
#region 匯入Excel表,返回DataTable
/// <summary>
/// 匯入Excel表,返回DataTable
/// </summary>
/// <param name="strFilePath">要匯入的Excel表</param>
/// <returns>返回DataTable類型的執行結果</returns>
public DataTable LendInDT(string strFilePath)
{
if (strFilePath == null)
{
throw new ArgumentNullException("filename string is null!");
}
if (strFilePath.Length == 0)
{
throw new ArgumentException("filename string is empty!");
}
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += strFilePath;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter da = null;
DataTable m_tableName = new DataTable(); ;
DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString();
}
string sqlMaster = " SELECT * FROM [" + m_tableName + "]";
da = new OleDbDataAdapter(sqlMaster, oleDBConn);
try
{
da.Fill(ds, "tb");
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
oleDBConn.Close();
oleDBConn.Dispose();
da.Dispose();
}
DataTable result = ds.Tables["tb"];
return result;
}
#endregion
}
}