asp.net中数据库操作通用类
Posted 0x16
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net中数据库操作通用类相关的知识,希望对你有一定的参考价值。
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
/// <summary>
/// SQLConnection 的摘要说明
/// </summary>
public class SQLConn
{
public SQLConn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection connstr; // 连接字符串
#region getStrConnection 获取连接字符串
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns></returns>
public string getStrConnection()
{
string constr;
constr = System.Configuration.ConfigurationSettings.AppSettings[ " strSQLConnection " ];
return constr;
}
#endregion
#region openConn() 打开数据库
/// <summary>
/// 打开数据库
/// </summary>
public void openConn()
{
string constr;
constr = getStrConnection();
connstr = new SqlConnection(constr);
connstr.Open();
}
#endregion
#region closeConn() 关闭数据库
/// <summary>
/// closeConn关闭数据库
/// </summary>
public void closeConn()
{
connstr.Dispose();
connstr.Close();
}
#endregion
#region execSQL(string sql) 执行sql语句
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="sql"></param>
public void execSQL( string sql)
{
openConn();
SqlCommand cmd = new SqlCommand(sql,connstr);
cmd.ExecuteNonQuery();
closeConn();
}
#endregion
#region getDataSet(string sql) 返回DataSet对象
/// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet getDataSet( string sql)
{
openConn();
SqlDataAdapter rs = new SqlDataAdapter(sql,connstr);
DataSet ds = new DataSet();
rs.Fill(ds);
return ds;
}
#endregion
#region getDataView(string sql) 返回DataView对象
/// <summary>
/// 返回DataView对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataView getDataView( string sql)
{
DataSet ds = new DataSet();
ds = getDataSet(sql);
DataView dv = new DataView(ds.Tables[ 0 ]);
return dv;
}
#endregion
#region getDataReader(string sql) 返回DataReader对象
/// <summary>
/// 返回DataReader对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader getDataReader( string sql)
{
openConn();
SqlCommand cmd = new SqlCommand(sql,connstr);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
#endregion
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
/// <summary>
/// SQLConnection 的摘要说明
/// </summary>
public class SQLConn
{
public SQLConn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection connstr; // 连接字符串
#region getStrConnection 获取连接字符串
/// <summary>
/// 获取连接字符串
/// </summary>
/// <returns></returns>
public string getStrConnection()
{
string constr;
constr = System.Configuration.ConfigurationSettings.AppSettings[ " strSQLConnection " ];
return constr;
}
#endregion
#region openConn() 打开数据库
/// <summary>
/// 打开数据库
/// </summary>
public void openConn()
{
string constr;
constr = getStrConnection();
connstr = new SqlConnection(constr);
connstr.Open();
}
#endregion
#region closeConn() 关闭数据库
/// <summary>
/// closeConn关闭数据库
/// </summary>
public void closeConn()
{
connstr.Dispose();
connstr.Close();
}
#endregion
#region execSQL(string sql) 执行sql语句
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="sql"></param>
public void execSQL( string sql)
{
openConn();
SqlCommand cmd = new SqlCommand(sql,connstr);
cmd.ExecuteNonQuery();
closeConn();
}
#endregion
#region getDataSet(string sql) 返回DataSet对象
/// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataSet getDataSet( string sql)
{
openConn();
SqlDataAdapter rs = new SqlDataAdapter(sql,connstr);
DataSet ds = new DataSet();
rs.Fill(ds);
return ds;
}
#endregion
#region getDataView(string sql) 返回DataView对象
/// <summary>
/// 返回DataView对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataView getDataView( string sql)
{
DataSet ds = new DataSet();
ds = getDataSet(sql);
DataView dv = new DataView(ds.Tables[ 0 ]);
return dv;
}
#endregion
#region getDataReader(string sql) 返回DataReader对象
/// <summary>
/// 返回DataReader对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public SqlDataReader getDataReader( string sql)
{
openConn();
SqlCommand cmd = new SqlCommand(sql,connstr);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
#endregion
}
以上是关于asp.net中数据库操作通用类的主要内容,如果未能解决你的问题,请参考以下文章