类代码
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);
}
}
}
用起来挺方便的!