程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#小軟件(SaveClassic)開發手記--(3)基礎類(數據訪問類DataAccess)

c#小軟件(SaveClassic)開發手記--(3)基礎類(數據訪問類DataAccess)

編輯:C#入門知識

 

好些日子沒整理自己的筆記了,實在是自己太忙了。我知道著不是借口,真是很累,根本就沒有時間精力去整理這些筆記,現在稍微有點時間,我趕快整理一下思路。我想了想,今天還是把一些基本的類整理一下吧,這些都是我們在平常開發中經常見到的。這些內容,我自己感覺會有些毛病,希望大家幫忙改進一下,謝謝。

 

一、數據訪問類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 www.2cto.com

 

        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;

 

        }

 

    }

 

}

 

這個類也許代碼會有很多問題,請大家幫忙指正。

 

作者 zhaoyang

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved