using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace zz
{
/// <summary>
/// ClassConn 的摘要说明。
/// </summary>
public class conn //数据库连接类
{
public conn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection connstr; //连接字符串
public string getconnstr() //获取连接字符串
{
string constr;
constr=System.Configuration.ConfigurationSettings.AppSettings["connstring"];
return constr;
}
public void open() //打开数据库
{
string constr;
constr=getconnstr();
connstr=new SqlConnection(constr);
connstr.Open();
}
public void close() //关闭数据库
{
connstr.Dispose();
connstr.Close();
}
public void execsql(string sql) //执行sql语句
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
cmd.ExecuteNonQuery();
close();
}
public DataSet dataset(string sql) //返回DataSet对象
{
open();
SqlDataAdapter rs=new SqlDataAdapter(sql,connstr);
DataSet ds=new DataSet();
rs.Fill(ds);
return ds;
}
public DataView dataview(string sql) //返回DataView对象
{
DataSet ds=new DataSet();
ds=dataset(sql);
DataView dv=new DataView(ds.Tables[0]);
return dv;
}
public SqlDataReader datareader(string sql) //返回DataReader对象
{
open();
SqlCommand cmd=new SqlCommand(sql,connstr);
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}
}
}
2. 连接oledb数据库
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace xyl
{
/// <summary>
/// ClassConn 的摘要说明。
/// </summary>
public class ClassConn
{
public ClassConn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public OleDbConnection connstr;
public string getconnstr()
{
string constr;
constr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+HttpRuntime.AppDomainAppPath+ System.Configuration.ConfigurationSettings.AppSettings["connstring"];
return constr;
}
public void OpenDB()
{
string constr;
constr=getconnstr();
connstr=new OleDbConnection(constr);
connstr.Open();
}
public void CloseDB()
{
connstr.Dispose();
connstr.Close();
}
public void execsql(string sql)
{
OpenDB();
OleDbCommand cmd=new OleDbCommand(sql,connstr);
cmd.ExecuteNonQuery();
CloseDB();
}
public DataSet DataSets(string sql)
{
OpenDB();
OleDbDataAdapter rs=new OleDbDataAdapter(sql,connstr);
DataSet ds=new DataSet();
rs.Fill(ds);
return ds;
}
public DataView DataViews(string sql)
{
DataSet ds=new DataSet();
ds=DataSets(sql);
DataView dv=new DataView(ds.Tables[0]);
return dv;
}
public OleDbDataReader MyDataReader(string sql)
{
OpenDB();
OleDbCommand myCom=new OleDbCommand(sql,connstr);
OleDbDataReader myreader=myCom.ExecuteReader();
return myreader;
}
}
}
Asp.net 数据库连接类及常用类,方法代码
最新推荐文章于 2021-04-13 08:22:32 发布