数据库连接类(一)
时间:2007-06-26 来源:chenqiubite
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; /// <summary>
/// TSqlDB 的摘要说明
/// </summary>
namespace _lib
{
/// <summary>
/// 注释
/// </summary>
public class TSqlDB
{
private string FConnString;
private string FQueryString; private SqlConnection FConnection;
private SqlCommand FCommand;
private SqlDataAdapter FDataAdapter;
private DataSet FDataSet; #region 构造方法
/// <summary>
/// 不带参数的构造方法
/// </summary>
public TSqlDB()
{
FConnString = GetConnString();
Initial("");
} /// <summary>
/// 带参数的构造方法
/// </summary>
/// <param name="Query"></param>
public TSqlDB(string Query)
{
FConnString = GetConnString();
Initial(Query);
}
#endregion #region 私有方法
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns></returns>
protected static string GetConnString()
{
return System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
} /// <summary>
/// 初始化:建立连接、命令、适配器、结果集对象
/// </summary>
private void Initial(string init_sql)
{
FConnection = new SqlConnection(FConnString);
FConnection.Open(); FCommand = new SqlCommand();
FCommand.Connection = FConnection;
FCommand.CommandText = init_sql;
FQueryString = init_sql; FDataAdapter = new SqlDataAdapter();
FDataAdapter.SelectCommand = FCommand; FDataSet = new DataSet();
if (init_sql.Length > 0)
{
FDataAdapter.Fill(FDataSet);
} }
#endregion #region 公有方法
/// <summary>
/// 释放数据库连接
/// </summary>
public void Free()
{
if (FDataSet != null)
FDataSet.Dispose(); if (FConnection != null)
{
FConnection.Close();
FConnection.Dispose();
FConnection = null;
} if (FCommand != null)
FCommand.Dispose(); if (FDataAdapter != null)
FDataAdapter.Dispose();
}
#endregion
}
}
这是数据库连接类的一部分,有了这部分接下来就可以写相关的数据库操作(查询、插入、更新和删除)
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; /// <summary>
/// TSqlDB 的摘要说明
/// </summary>
namespace _lib
{
/// <summary>
/// 注释
/// </summary>
public class TSqlDB
{
private string FConnString;
private string FQueryString; private SqlConnection FConnection;
private SqlCommand FCommand;
private SqlDataAdapter FDataAdapter;
private DataSet FDataSet; #region 构造方法
/// <summary>
/// 不带参数的构造方法
/// </summary>
public TSqlDB()
{
FConnString = GetConnString();
Initial("");
} /// <summary>
/// 带参数的构造方法
/// </summary>
/// <param name="Query"></param>
public TSqlDB(string Query)
{
FConnString = GetConnString();
Initial(Query);
}
#endregion #region 私有方法
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns></returns>
protected static string GetConnString()
{
return System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
} /// <summary>
/// 初始化:建立连接、命令、适配器、结果集对象
/// </summary>
private void Initial(string init_sql)
{
FConnection = new SqlConnection(FConnString);
FConnection.Open(); FCommand = new SqlCommand();
FCommand.Connection = FConnection;
FCommand.CommandText = init_sql;
FQueryString = init_sql; FDataAdapter = new SqlDataAdapter();
FDataAdapter.SelectCommand = FCommand; FDataSet = new DataSet();
if (init_sql.Length > 0)
{
FDataAdapter.Fill(FDataSet);
} }
#endregion #region 公有方法
/// <summary>
/// 释放数据库连接
/// </summary>
public void Free()
{
if (FDataSet != null)
FDataSet.Dispose(); if (FConnection != null)
{
FConnection.Close();
FConnection.Dispose();
FConnection = null;
} if (FCommand != null)
FCommand.Dispose(); if (FDataAdapter != null)
FDataAdapter.Dispose();
}
#endregion
}
}
这是数据库连接类的一部分,有了这部分接下来就可以写相关的数据库操作(查询、插入、更新和删除)
相关阅读 更多 +