C# 操作access数据库

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

posted @ 2010-11-24 10:48  scgw  阅读(424)  评论(0编辑  收藏  举报