代码改变世界

c#小软件(SaveClassic)开发手记--(3)基础类(数据访问类DataAccess)

2011-12-07 09:32  zhaoyang  阅读(1531)  评论(2编辑  收藏  举报

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

一、数据访问类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;

        }

    }

}

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