Asp.net访问Access数据库1-访问类

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

///
///oledbAccessHelper 的摘要说明
///
public class oledbAccessHelper
{
    private string oledbstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
    OleDbConnection myconn=null;
    public oledbAccessHelper(string dbPath)
 {
        myconn = new OleDbConnection(oledbstr + dbPath + ";Jet OLEDB:Database Password=123456");
 }
    public bool oledbSqlToDataTable(string sqlstr,ref DataTable mytab)
    {
        bool jg = false;
        try
        {
            using (OleDbDataAdapter myadp = new OleDbDataAdapter(sqlstr, myconn))
            {
                myconn.Open();
                DataSet mydst = new DataSet();
                myadp.Fill(mydst);
                if (mydst.Tables.Count >0)
                {
                    jg = true;
                    mytab = mydst.Tables[0];
                }               
            }
        }
        catch(Exception err)
        {
            mytab.Clear();
            mytab.Rows.Add("errmsg");
            DataRow myr = mytab.NewRow();
            myr["errmsg"] =err.Message ;
        }
        finally
        {
            try { myconn.Close(); }
            catch { }
        }
        return jg;
    }
    public bool oledbSqlToDataTable(string sqlstr, OleDbParameter[] mypatams, ref DataTable mytab)
    {
        bool jg = false;
        try
        {
            using (OleDbCommand mycmd = new OleDbCommand(sqlstr, myconn))
            {

                foreach (OleDbParameter mp in mypatams)
                {
                    mycmd.Parameters.Add(mp);
                }
                using (OleDbDataAdapter myadp = new OleDbDataAdapter(mycmd))
                {
                    myconn.Open();
                    DataSet mydst = new DataSet();
                    myadp.Fill(mydst);
                    if (mydst.Tables.Count > 0)
                    {
                        jg = true;
                        mytab = mydst.Tables[0];
                    }
                }
            }
        }
        catch (Exception err)
        {
            mytab.Clear();
            mytab.Rows.Add("errmsg");
            DataRow myr = mytab.NewRow();
            myr["errmsg"] = err.Message;
        }
        finally
        {
            try { myconn.Close(); }
            catch { }
        }
        return jg;
    }
    public bool oledbSqlZhixing(string sqlstr, ref string msgStr)
    {
        bool jg = false;
        try
        {
            using (OleDbCommand mycmd = new OleDbCommand(sqlstr, myconn))
            {
                myconn.Open();
                mycmd.ExecuteNonQuery();
                jg = true;
                msgStr = "执行成功";
            }
        }
        catch(Exception err)
        { msgStr = err.Message; }
        finally
        {
            try { myconn.Close(); }
            catch { }
        }
        return jg;
    }
    public bool oledbSqlZhixing(string sqlstr,OleDbParameter[] mypatams, ref string msgStr)
    {
        bool jg = false;
        try
        {
            using (OleDbCommand mycmd = new OleDbCommand(sqlstr, myconn))
            {
                foreach (OleDbParameter mp in mypatams)
                {
                    mycmd.Parameters.Add(mp);
                }
                myconn.Open();
                mycmd.ExecuteNonQuery();
                jg = true;
                msgStr = "执行成功";
            }
        }
        catch (Exception err)
        { msgStr = err.Message; }
        finally
        {
            try { myconn.Close(); }
            catch { }
        }
        return jg;
    }
}

Asp.net访问Access数据库1-访问类,布布扣,bubuko.com

Asp.net访问Access数据库1-访问类

上一篇:编程使用c#连接到IBM db2的两种方式


下一篇:oracle跟踪