网上有很多数据库访问的代码,我也整理了一点,希望有高手可以指点一二
访问基类
namespace Alawn.ClassLibrary.Database
{
/// <summary>
/// BasicClass 为数据库联接基类。
/// </summary>
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class BasicClass :IDisposable
{
protected SqlConnection m_oConnection;
protected SqlCommand m_oCommad;
protected SqlDataReader m_oSqlDataReader;
protected DataSet m_oDataSet;
protected SqlDataAdapter m_oSqlDataAdapter;
protected DataView m_oDataView;
protected string m_ConnectionString ;

public BasicClass()
{
m_ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
this.m_oCommad = new SqlCommand();
this.m_oSqlDataAdapter = new SqlDataAdapter();
this.m_oDataSet = new DataSet();
}

/// <summary>
/// 打开数据库连接
/// </summary>
protected void Open()
{
if(this.m_oConnection == null)
this.m_oConnection = new SqlConnection(this.m_ConnectionString);
if(this.m_oConnection.State == System.Data.ConnectionState.Closed)
this.m_oConnection.Open();
}

/// <summary>
/// 关闭数据库连接
/// </summary>
protected void Close()
{
if(this.m_oConnection != null)
this.m_oConnection.Close();
}

/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (m_oConnection != null)
{
m_oConnection.Dispose();
m_oConnection = null;
}
}

}
}
访问基类
namespace Alawn.ClassLibrary.Database
{
/// <summary>
/// BasicClass 为数据库联接基类。
/// </summary>
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class BasicClass :IDisposable
{
protected SqlConnection m_oConnection;
protected SqlCommand m_oCommad;
protected SqlDataReader m_oSqlDataReader;
protected DataSet m_oDataSet;
protected SqlDataAdapter m_oSqlDataAdapter;
protected DataView m_oDataView;
protected string m_ConnectionString ;
public BasicClass()
{
m_ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
this.m_oCommad = new SqlCommand();
this.m_oSqlDataAdapter = new SqlDataAdapter();
this.m_oDataSet = new DataSet();
}
/// <summary>
/// 打开数据库连接
/// </summary>
protected void Open()
{
if(this.m_oConnection == null)
this.m_oConnection = new SqlConnection(this.m_ConnectionString);
if(this.m_oConnection.State == System.Data.ConnectionState.Closed)
this.m_oConnection.Open();
}
/// <summary>
/// 关闭数据库连接
/// </summary>
protected void Close()
{
if(this.m_oConnection != null)
this.m_oConnection.Close();
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (m_oConnection != null)
{
m_oConnection.Dispose();
m_oConnection = null;
}
}
}
}


浙公网安备 33010602011771号