数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

一个简单的数据库操作类


发布日期:2018年01月29日
 
一个简单的数据库操作类

类代码

using System;

using SystemData;

using SystemDataSqlClient;

namespace exchange

{

public class MyDataOp

{

private String StrSql;

private String StrConn;

private SqlConnection Conn;

public MyDataOp(String ss)

{

StrSql = ss;

StrConn = SystemConfigurationConfigurationSettingsAppSettings[ConnectionString];

}

public SqlDataReader CreateReader()

{

Conn=new SqlConnection(StrConn);

SqlCommand Comm=new SqlCommand(StrSqlConn);

ConnOpen();

SqlDataReader MyReader = CommExecuteReader();

return MyReader;

}

public DataSet CreateDataSet()

{

Conn=new SqlConnection(StrConn);

ConnOpen();

SqlDataAdapter Adpt=new SqlDataAdapter(StrSqlConn);

DataSet Ds=new DataSet();

AdptFill(Ds);

return Ds;

}

public SqlCommand ExecuteCommand()

{

Conn=new SqlConnection(StrConn);

SqlCommand Comm=new SqlCommand(StrSqlConn);

ConnOpen();

CommExecuteNonQuery();

return Comm;

}

public void Close()

{

ConnClose();

}

}

}

基本用法

namespace exchange

{

using System;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemData;

using SystemDataSqlClient;

using SystemWeb;

using SystemSecurityCryptography;

public class newjoin : SystemWebUIUserControl

{

protected DataList newjoinlist;

public newjoin()

{

thisInit += new SystemEventHandler(Page_Init);

}

protected void Page_Load(object sender SystemEventArgs e)

{

if(!IsPostBack)

{

LoadData();

}

}

protected void LoadData()

{

String StrSql = select top idleft(corpname) as corpname from crop order by id desc;

MyDataOp mdo = new MyDataOp(StrSql);

DataSet ds = mdoCreateDataSet();

newjoinlistDataSource =dsTables[]DefaultView;

newjoinlistDataBind();

mdoClose();

}

private void Page_Init(object sender EventArgs e)

{

InitializeComponent();

}

private void InitializeComponent()

{

thisLoad += new SystemEventHandler(thisPage_Load);

}

}

}               

上一篇:数据访问技术

下一篇:数据仓库的逻辑结构和物理结构