c# mysql类使用方法_【转贴】 C#数据库类的使用方法

///Db_Class 的摘要说明。///publicclassDb_Class

{publicOleDbConnection Conn;//构造函数publicDb_Class()

{

Conn=newOleDbConnection("Provider=SQLOLEDB;Server=(local);Pwd=123456;UID=sa;Database=test");

}//打开数据源链接publicOleDbConnection Db_Conn()

{

Conn.Open();returnConn;

}//返回DataReader数据集,下面的SQL可以动态生成publicOleDbDataReader Db_CreateReader(stringSQL)

{

Db_Conn();

OleDbCommand cmd=newOleDbCommand(SQL,Conn);

OleDbDataReader Rs=cmd.ExecuteReader();returnRs;this.close();

}//返回DataReader数据集,下面的SQL是存储过程publicOleDbDataReader Db_CommandReader(stringSQL)

{

Db_Conn();

OleDbCommand cmd=newOleDbCommand(SQL,Conn);

cmd.CommandType=CommandType.StoredProcedure;

OleDbDataReader Rs=cmd.ExecuteReader();returnRs;this.close();

}//返回数据DataSet数据集publicOleDbDataSet Db_CreateDataSet(stringSQL)

{

Db_Conn();

OleDbCommand cmd=newOleDbCommand(SQL,Conn);

OleDbDataAdapter Adpt=newOleDbDataAdapter(cmd,Conn);

DataSet Ds=newDataSet();

Adpt.Fill(Ds,"NewTable");returnDs;this.close();

}//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数publicboolDb_ExecuteNonquery(stringSQL)

{

Db_Conn();

OleDbCommand cmd=newOleDbCommand(SQL,Conn);try{

cmd.ExecuteNonQuery();returntrue;

}catch{returnfalse;

}this.close();

}//关闭数据链接publicvoidclose()

{

Conn.Close();

}

}

}

使用方法如下:usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Web;usingSystem.Web.SessionState;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.HtmlControls;namespaceDbClass

{//WebForm1 的摘要说明。///publicclassWebForm1 : System.Web.UI.Page

{protectedSystem.Web.UI.WebControls.DataGrid DataGrid1;protectedSystem.Web.UI.WebControls.Button Button1;privatevoidPage_Load(objectsender, System.EventArgs e)

{//在此处放置用户代码以初始化页面//string SQL="select * from sysfiles";Db_Class Db_class=newDb_Class();

DataGrid1.DataSource=Db_class.Db_CommandReader("sp_tables");//使用SQLSERVER的存储过程。DataGrid1.DataBind();

}#regionWeb 窗体设计器生成的代码overrideprotectedvoidOnInit(EventArgs e)

{CODEGEN: 该调用是 asp.net Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);

}//设计器支持所需的方法 - 不要使用代码编辑器修改///此方法的内容。///privatevoidInitializeComponent()

{this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);this.DataGrid1.SelectedIndexChanged+=newSystem.EventHandler(this.DataGrid1_SelectedIndexChanged);this.Load+=newSystem.EventHandler(this.Page_Load);

}#endregion}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值