操作MDB

  using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class OleDbHelper
    {
        private const string accessPrefix = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=Admin;Jet OLEDB:Database Password=;";
        private string connectionString = "";

        public OleDbHelper(string accessFilePath)
        {
            this.connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=Admin;Jet OLEDB:Database Password=;", accessFilePath);
        }

        public int ExecuteNonQuery(List<string> sqlList)
        {
            int num = 0;
            using (OleDbConnection connection = new OleDbConnection(this.connectionString))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.set_Connection(connection);
                foreach (string str in sqlList)
                {
                    command.CommandText = str;
                    command.CommandType = CommandType.Text;
                    try
                    {
                        command.ExecuteNonQuery();
                        num++;
                        continue;
                    }
                    catch (Exception exception)
                    {
                        LogHelper.Error(exception);
                        continue;
                    }
                }
            }
            return num;
        }

        public IDataReader ExecuteReader(string sql)
        {
            IDataReader reader = null;
            OleDbConnection connection = new OleDbConnection(this.connectionString);
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.set_Connection(connection);
            command.CommandText = sql;
            command.CommandType = CommandType.Text;
            try
            {
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exception)
            {
                LogHelper.Error(exception);
            }
            return reader;
        }
    }

posted on 2012-03-25 22:44  西湖浪子  阅读(150)  评论(0)    收藏  举报