---
title: "C# 采集3-采集，access一般操作基类"
author: "lolocomee"
date: "2010-07-24T10:29:49Z"
tags: []
categories: ["编程语言"]
slug: "c-3-access"
draft: false
lastmod: "2010-07-24T10:29:49Z"
id: 720252
url: "https://lolocomee.iteye.com/blog/720252"
---

要用到access存储数据，不过是简单的操作，顺便温习一下。若是.net framework 版本高，可以直接用linkQ。我用vs2005，所以还是自己写比较放心，很简单的一个。

```
  public class Dao
    {
        public string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "/data.mdb";
        public DataTable tb = null;
        public DataSet ds = null;
        public OleDbCommand dc = null;
        public OleDbConnection conn = null;
        public OleDbDataAdapter odda = null;


        /// <summary>
        /// 打开连接
        /// </summary>
        public void OpenConn()
        {
            if (conn != null)
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            else
            {
                conn = new OleDbConnection(strCon);
                conn.Open();
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public void DisposeConn()
        {
            if (conn != null)
            {
                conn.Close();
            }
            if (ds != null)
            {
                ds.Dispose();
            }
            if (odda != null)
            {
                odda.Dispose();
            }
        }

        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public int ExceteQuery(String sqlstr)
        {
            int val = 0;
            dc = new OleDbCommand(sqlstr, conn);
            val = dc.ExecuteNonQuery();
            return val;
        }

        /// <summary>
        /// 执行sql 返回dataset
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(String sqlstr)
        {
            dc = new OleDbCommand(sqlstr, conn);
            odda = new OleDbDataAdapter(dc);
            ds = new DataSet();
            odda.Fill(ds);
            return ds;
        }
    }
```