Simplify DatabaseAccess

本文介绍了一种简化数据库操作的方法,通过创建DataAccessBase基类来减少重复代码,该基类负责处理SqlConnection、SqlCommand和SqlDataReader对象,使得继承此类的类可以轻松执行SQL命令或获取数据表。
 

Introduction

As a developer, I often write the same code over and over again. SqlConnection and SqlCommand are frequently used objects and it's boring to repeat almost the same code. The only difference is the commandText and the parameters. And of course what the datareader does. See the code and you will get the point.

Using the Code

The main thing here is that you inherit a DataAccessBase which does most of the work. The DataAccessBase class creates the three objects you need to do most database operations:

  • SqlConnection
  • SqlCommand
  • SqlDataReader

Putting all dataaccess handling in the same class like this will make it easy to modify it for performance or even change databaseprovider.

In a class which inherits from the class DataAccessBase, you can easily execute commandtext, get datatables from your database, etc. The following code gives you an idea of how this works:

Collapse
// A class with two simple methods for some dataaccess the normal way...
public class NormalDataAccess
{
    // A simple database example. Add a user
    // Uses a stored procedure which returns UserId
    public int AddUser(string username, string password)
    {
        SqlConnection conn = new SqlConnection("connectionString");
        SqlCommand cmd = new SqlCommand("spMyProcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@UserId", DBNull.Value));
        cmd.Parameters["@UserId"].SqlDbType = SqlDbType.Int;
        cmd.Parameters["@UserId"].Size = 4;
        cmd.Parameters["@UserId"].Direction = ParameterDirection.Output;
        cmd.Parameters.Add(new SqlParameter("@Username", username));
        cmd.Parameters.Add(new SqlParameter("@Password", password));

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        int userId = Convert.ToInt32(cmd.Parameters["@UserId"].Value);

        cmd.Dispose();
        conn.Dispose();

        return userId;
    }

    // Just execute a simple sqlcommand
    public void ExecuteSqlCommand()
    {
        SqlConnection conn = new SqlConnection("connectionString");
        SqlCommand cmd = new SqlCommand("spMyProcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        cmd.Dispose();
        conn.Dispose();
    }
}

// The same two methods but with DataAccessBase inherited
public class SimplifiedDataAccess : DataAccessBase
{
    public int AddUser(string username, string password)
    {
        base.AddParameter("@UserId", DBNull.Value, SqlDbType.Int, 4, ParameterDirection.Output);
        base.AddParameter("@Username", username, SqlDbType.VarChar, 255);
        base.AddParameter("@Password", password, SqlDbType.VarChar, 255);
        base.ExecuteNonQuery("spUsers_AddUser");
        int UserId = Convert.ToInt32(base.GetParameter("@UserId").Value);
        base.Dispose();
        return UserId;
    }

    public void ExecuteSqlCommand()
    {
        base.ExecuteNonQuery("spMyProcedure");
        base.Dispose();
    }
}

And the DataAccessBase looks like this:
(I have removed comments. Download the example to see more.)

Collapse
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SimplifyDataAccess
{

    public abstract class DataAccessBase
    {
        public SqlConnection Connection;
        public SqlCommand Command;
        public SqlDataReader DataReader;

        public DataAccessBase()
        {
            Connection = new SqlConnection("### Your connectionstring ###");
            Command = new SqlCommand("", Connection);
            Command.CommandType = CommandType.StoredProcedure;
        }

        public void Dispose()
        {
            if ((DataReader != null) && (!DataReader.IsClosed))
                DataReader.Close();

            if (Connection.State == ConnectionState.Open)
                Connection.Close();

            DataReader = null;
            Command.Dispose();
            Command = null;
            Connection.Dispose();
            Connection = null;
        }

        public void ExecuteDataReader(string commandText)
        {
            if (Connection.State == ConnectionState.Closed)
                Connection.Open();

            this.Command.CommandText = commandText;
            this.DataReader = this.Command.ExecuteReader();
        }

        public void ExecuteNonQuery(string commandText)
        {
            Command.CommandText = commandText;

            if (Connection.State == ConnectionState.Open)
            {
                Command.ExecuteNonQuery();
            }
            else
            {
                Connection.Open();
                Command.ExecuteNonQuery();
                Connection.Close();
            }
        }

        public object ExecuteScalar(string commandText)
        {
            Command.CommandText = commandText;
            object value = null;

            if (Connection.State == ConnectionState.Open)
            {
                value = Command.ExecuteScalar();
            }
            else
            {
                Connection.Open();
                value = Command.ExecuteScalar();
                Connection.Close();
            }

            return value;
        }

        public DataTable ExecuteDataTable(string commandText, string tableName)
        {
            Command.CommandText = commandText;
            DataTable dt = null;

            if (Connection.State == ConnectionState.Open)
            {
                SqlDataAdapter da = new SqlDataAdapter(Command);
                dt = new DataTable(tableName);
                da.Fill(dt);
            }
            else
            {
                Connection.Open();
                SqlDataAdapter da = new SqlDataAdapter(Command);
                dt = new DataTable(tableName);
                da.Fill(dt);
                Connection.Close();
            }

            return dt;
        }

        public void AddParameter(string parameterName, object value, 
                SqlDbType sqlDbType, int size)
        {
            AddParameter(parameterName, value, sqlDbType, size, 
                ParameterDirection.Input);
        }

        public void AddParameter(string parameterName, object value, 
                SqlDbType sqlDbType, int size, ParameterDirection direction)
        {
            SqlParameter p = new SqlParameter(parameterName, value);
            p.SqlDbType = sqlDbType;
            p.Size = size;
            p.Direction = direction;
            Command.Parameters.Add(p);
        }

        public SqlParameter GetParameter(string parameterName)
        {
            return this.Command.Parameters[parameterName];
        }
    }
}
 

History

  • 31st January, 2008: This is the first version and I am using this method in most of my own portals and it works perfectly.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Serverside





My personal homepage serverside.no. (Norwegian developer portal)
Occupation: Software Developer (Senior)
Company: ONE Software AS - www.onesoftware.no
Location: Norway Norway
内容概要:本文系统研究了直流微网中直流母线电压恢复的二次控制策略,重点提出并实现了基于虚拟压降补偿的方法在并联双向Buck-boost变换器中的应用。通过Simulink搭建详细的仿真模型,深入分析了虚拟压降原理及其在多变换器并联系统中的协调控制机制,有效解决了因线路阻抗差异导致的电压偏差与电流分配不均问题,实现了母线电压的精确调节与快速恢复,显著提升了系统的稳定性、均流性能与电能质量。研究涵盖了控制策略设计、关键参数整定及动态响应特性验证,提供了完整的仿真流程与结果分析。; 适合人群:具备电力电子、自动控制及微电网相关专业知识背景,熟悉Simulink仿真环境,从事新能源发电、直流配电系统、分布式能源控制等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入理解直流微网中母线电压稳定与均流控制的关键技术;②掌握虚拟压降补偿在二次控制中的理论基础与实现方法;③构建并调试并联Buck-boost变换器的协同控制系统仿真模型,服务于学术研究、课程设计或实际工程项目开发; 阅读建议:学习过程中应结合Simulink模型细致剖析控制回路结构,重点关注虚拟阻抗参数对系统动态性能与鲁棒性的影响,建议通过改变负载工况、线路参数或增加变换器数量等方式进行对比仿真,以全面评估控制策略的有效性与适应性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值