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

  • 相关阅读:
    Swift -- 8.3 多态
    Swift -- 8.2 类的构造与析构
    Swift -- 8.1 继承
    Swift -- 7.6 构造器
    Swift -- 7.5 类型属性,方法
    Swift -- 7.4 方法,下标,可选链
    Swift -- 7.3 类和结构体
    Swift -- 7.2 枚举
    Swift -- 7.1 面向对象简介
    4-5轮选区的不透明度1.7
  • 原文地址:https://www.cnblogs.com/scgw/p/1886285.html
Copyright © 2020-2023  润新知