转 c#接简单数据库操作类

本文介绍了一个用于封装 SQL Server 数据库操作的 C# 类,包括连接数据库、执行查询及存储过程等功能,并提供了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类代码:

using System;
using System.Data;
using System.Data.OleDb;

namespace DbClass
{
///
/// Db_Class 的摘要说明。
///
public class Db_Class
{
public OleDbConnection Conn;
//构造函数
public Db_Class()
{
Conn= new OleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");
}
//打开数据源链接
public OleDbConnection Db_Conn()
{
Conn.Open();
return Conn;
}
//返回DataReader数据集,下面的SQL可以动态生成
public OleDbDataReader Db_CreateReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回DataReader数据集,下面的SQL是存储过程
public OleDbDataReader Db_CommandReader(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader Rs = cmd.ExecuteReader();
return Rs;
this.close();
}
//返回数据DataSet数据集
public OleDbDataSet Db_CreateDataSet(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
OleDbDataAdapter Adpt= new OleDbDataAdapter(cmd,Conn);
DataSet Ds = new DataSet();
Adpt.Fill(Ds,"NewTable");
return Ds;
this.close();
}
//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
public bool Db_ExecuteNonquery(string SQL)
{
Db_Conn();
OleDbCommand cmd = new OleDbCommand(SQL,Conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
this.close();
}
//关闭数据链接
public void close()
{
Conn.Close();
}

}
}
使用方法如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 DbClass
{
///
/// WebForm1 的摘要说明。
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string SQL="select * from sysfiles";
Db_Class Db_class = new Db_Class();
DataGrid1.DataSource=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。
DataGrid1.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-919654/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-919654/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值