首页 > 开发 > 综合 > 正文

一个简单的数据库操作类

2024-07-21 02:23:37
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

类代码

using system;
using system.data;
using system.data.sqlclient;


namespace exchange
{
public class mydataop
{
private string strsql;
private string strconn;
private sqlconnection conn;
public mydataop(string ss)
{
strsql = ss;
strconn = system.configuration.configurationsettings.appsettings["connectionstring"];
}

public sqldatareader createreader()
{
conn=new sqlconnection(strconn);
sqlcommand comm=new sqlcommand(strsql,conn);
conn.open();
sqldatareader myreader = comm.executereader();
return myreader;
}
public dataset createdataset()
{
conn=new sqlconnection(strconn);
conn.open();
sqldataadapter adpt=new sqldataadapter(strsql,conn);
dataset ds=new dataset();
adpt.fill(ds);
return ds;
}
public sqlcommand executecommand()
{
conn=new sqlconnection(strconn);
sqlcommand comm=new sqlcommand(strsql,conn);
conn.open();
comm.executenonquery();
return comm;
}
public void close()
{
conn.close();
}
}
}



基本用法

namespace exchange
{
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data;
using system.data.sqlclient;
using system.web;
using system.security.cryptography;


public class newjoin : system.web.ui.usercontrol
{

protected datalist newjoinlist;
public newjoin()
{
this.init += new system.eventhandler(page_init);
}

protected void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
loaddata();
}
}

protected void loaddata()
{
string strsql = "select top 10 id,left(corpname,13) as corpname from crop order by id desc";
mydataop mdo = new mydataop(strsql);
dataset ds = mdo.createdataset();
newjoinlist.datasource = ds.tables[0].defaultview;
newjoinlist.databind();
mdo.close();
}

private void page_init(object sender, eventargs e)
{
initializecomponent();
}
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表