zoukankan      html  css  js  c++  java
  • 连接本地Access数据库,返回数据集DataSet

            #region 连接本地数据库

            private OleDbConnection con;
            /// <summary>
            /// 打开数据库连接
            /// </summary>
            /// <param name="DBpath">数据库路径(包括数据库名)</param>
            private void Open(String DBpath)
            {
                if (con == null)
                    con = new OleDbConnection(@" Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =C:\Users\yuleyi\Desktop\LocalConfig.mdb");
                if (con.State == ConnectionState.Closed)
                    con.Open();
            }

            /// <summary>
            /// 创建一个命令对象并返回该对象
            /// </summary>
            /// <param name="sqlStr">数据库语句</param>
            /// <param name="file">数据库所在路径</param>
            /// <returns>OleDbCommand</returns>
            private OleDbCommand CreateCommand(string sqlStr, string file)
            {
                Open(file);
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sqlStr;
                cmd.Connection = con;
                return cmd;
            }

            /// <summary>
            /// 执行数据库查询
            /// </summary>
            /// <param name="sqlStr">查询语句</param>
            /// <param name="tableName">填充数据集表格的名称</param>
            /// <param name="file">数据库所在路径</param>
            /// <returns>查询的数据集</returns>
            public DataSet GetDataSet(string sqlStr, string file)
            {
                DataSet ds = new DataSet();
                OleDbCommand cmd = CreateCommand(sqlStr, file);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd);
                dataAdapter.Fill(ds);
                cmd.Dispose();          
                dataAdapter.Dispose();
                return ds;
            }

  • 相关阅读:
    Qt为啥从4.8直接就跳到5.3了呢?这不科学吧
    一段程序的人生 第10章: server
    项目记录23--unity-tolua框架MediatorManager
    Raft 为什么是更易理解的分布式一致性算法
    Caused by: java.lang.UnsatisfiedLinkError: Couldn&#39;t load BaiduMapVOS_v2_1_3: findLibrary returned nu
    Apache OFBIZ高速上手(二)--MVC框架
    Eclipse 导入逆向工程
    mysql 报错从 新安装
    maven项目创建4
    maven报错
  • 原文地址:https://www.cnblogs.com/sdustyuleyi/p/2683646.html
Copyright © 2011-2022 走看看