新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:c#接简单数据库操作类

c#接简单数据库操作类

新客网 XKER.COM 2004-06-17 来源: 收藏本文
类代码:

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

namespace DbClass
{
/// <summary>
/// Db_Class 的摘要说明。
/// </summary>
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
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
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);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
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
}
}


呵呵,第一次在CSDN上面发表文章,如果有什么不好的地方请指正,欢迎能够与大家一起讨论。QQ:171476439 email:soho_suport@163.COM
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐