zoukankan      html  css  js  c++  java
  • c#小软件(SaveClassic)开发手记(3)基础类(数据访问类DataAccess)

    好些日子没整理自己的笔记了,实在是自己太忙了。我知道着不是借口,真是很累,根本就没有时间精力去整理这些笔记,现在稍微有点时间,我赶快整理一下思路。我想了想,今天还是把一些基本的类整理一下吧,这些都是我们在平常开发中经常见到的。这些内容,我自己感觉会有些毛病,希望大家帮忙改进一下,谢谢。

    一、数据访问类DataAccess

    数据访问类是我这个小软件最基础的类,它主要完成的功能就是,实现了对Access数据库的访问操作,具体代码如下。

    using System.Data.OleDb;

    using System.Data;

    using System;

    namespace Common

    {

        public class DataAccess

        {

            protected static OleDbConnection conn = new OleDbConnection();

            protected static OleDbCommand comm = new OleDbCommand();

            public static string connstring = "";

            public DataAccess()

            {

            }

            private static void openConnection()

            {

                if (conn.State == ConnectionState.Closed)

                {

                    conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + connstring;

                    comm.Connection = conn;

                try

                {

                conn.Open();

                }

                catch (Exception e)

                { throw new Exception(e.Message); }

                }

            }

     

            private static void closeConnection()

            {

                if (conn.State == ConnectionState.Open)

                {

                    conn.Close();

                    conn.Dispose();

                    comm.Dispose();

                }

            }

     

            public static void excuteSql(string sqlstr)

            {

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    comm.ExecuteNonQuery();

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                { closeConnection(); }

            }//执行sql语句

     

            public static OleDbDataReader dataReader(string sqlstr)

            {

                OleDbDataReader dr = null;

                try

                {

                    openConnection();

                    comm.CommandText = sqlstr;

                    comm.CommandType = CommandType.Text;

     

                    dr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                }

                catch

                {

                    try

                    {

                        dr.Close();

                        closeConnection();

                    }

                    catch { }

                }

                return dr;

            }//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。

            public static void dataReader(string sqlstr, ref OleDbDataReader dr)

            {

                try

                {

                    openConnection();

                    comm.CommandText = sqlstr;

                    comm.CommandType = CommandType.Text;

                    dr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                }

                catch

                {

                    try

                    {

                        if (dr != null && !dr.IsClosed)

                            dr.Close();

                    }

                    catch

                    {

                    }

                    finally

                    {

                        closeConnection();

                    }

                }

            }//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭

            public static DataSet dataSet(string sqlstr)

            {

                DataSet ds = new DataSet();

                OleDbDataAdapter da = new OleDbDataAdapter();

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    da.SelectCommand = comm;

                    da.Fill(ds);

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                {

                    closeConnection();

                }

                return ds;

            }//返回指定sql语句的dataset

     

            public static void dataSet(string sqlstr, ref DataSet ds)

            {

                OleDbDataAdapter da = new OleDbDataAdapter();

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    da.SelectCommand = comm;

                    da.Fill(ds);

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                {

                    closeConnection();

                }

            }//返回指定sql语句的dataset

            public static DataTable dataTable(string sqlstr)

            {

                DataTable dt = new DataTable();

                OleDbDataAdapter da = new OleDbDataAdapter();

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    da.SelectCommand = comm;

                    da.Fill(dt);

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                {

                    closeConnection();

                }

                return dt;

            }//返回指定sql语句的datatable

            public static void dataTable(string sqlstr, ref DataTable dt)

            {

                OleDbDataAdapter da = new OleDbDataAdapter();

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    da.SelectCommand = comm;

                    da.Fill(dt);

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                {

                    closeConnection();

                }

            }//返回指定sql语句的datatable

     

            public static DataView dataView(string sqlstr)

            {

                OleDbDataAdapter da = new OleDbDataAdapter();

                DataView dv = new DataView();

                DataSet ds = new DataSet();

                try

                {

                    openConnection();

                    comm.CommandType = CommandType.Text;

                    comm.CommandText = sqlstr;

                    da.SelectCommand = comm;

                    da.Fill(ds);

                    dv = ds.Tables[0].DefaultView;

                }

                catch (Exception e)

                {

                    throw new Exception(e.Message);

                }

                finally

                {

                    closeConnection();

                }

                return dv;

            }

        }

    }

    这个类也许代码会有很多问题,请大家帮忙指正。

  • 相关阅读:
    [转]如何有效运作知识库
    针式PKM如何帮助你解决个人知识管理中的常见问题?
    知识管理的目的是什么?如何进行知识管理?
    [转]阅读《讀書這玩意兒》有感&笔记
    [转]学习的三个层次
    小鱼干的做法
    IIS ERROR: Server Application Error 和 IIS与.NET Framework的安装顺序问题
    C#调用oracle存储过程 最简单的实例
    Oracle 中游标实例
    查看Sql Server是否有打SP4
  • 原文地址:https://www.cnblogs.com/studyplay/p/2278745.html
Copyright © 2011-2022 走看看