C# 操作access資料庫

來源:互聯網
上載者:User

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
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.