62,067
社区成员
发帖
与我相关
我的任务
分享
protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
public static string x;
public static string mySql = "select * from goods order by id asc";
protected static string cb;
protected static string cba;
WmsClass gl = new WmsClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ BindDataList(0);}
}
private void BindDataList(int currentpage)
{
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = currentpage;
DataSet ds = new DataSet();
ds=gl.GetDataSet(mySql);
pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
DataList.DataSource = pds;//绑定Datalist
DataList.DataBind();
}
protected void DataList_DeleteCommand(object source, DataListCommandEventArgs e)
{ x = ((Label)e.Item.FindControl("LB_id")).Text.Trim();
mySql = "delete from goods where id='" + x + "'";
gl.ExecuteSql(mySql);
BindDataList(0);
}
public class WmsClass
{
public WmsClass()
{
}
public SqlConnection con;
bool myDBConSelfOpen = false;
private void OpenSql()
{
if (con == null)
{
con = new SqlConnection(@"Data Source=GENGHI;Initial Catalog=WMS;Integrated Security=True");
con.Open();
myDBConSelfOpen = true;
}
}
private void CloseSql()
{
if (myDBConSelfOpen == true)
{
con.Close();
con.Dispose();
con = null;
myDBConSelfOpen = false;
}
}
public SqlDataReader myDataReader(string mySql)
{
SqlDataReader dr;
try
{
OpenSql();
SqlCommand cmd = new SqlCommand(mySql, con);
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
throw ex;
}
return dr;
}
public DataRow GetDataRow(string strQuery)
{
DataRow myRow = null;
try
{
DataTable myTable = GetDataTable(strQuery);
if (myTable.Rows.Count > 0)
{
myRow = myTable.Rows[0];
}
}
catch (Exception ex)
{
throw ex;
}
return myRow;
}
public DataSet GetDataSet(string strQuery)
{
DataSet myDataSet = new DataSet();
try
{
OpenSql();
SqlDataAdapter myDataAdapter = new SqlDataAdapter(strQuery, con);
myDataAdapter.Fill(myDataSet);
}
catch (Exception ex)
{
throw ex;
}
CloseSql();
return myDataSet;
}
public DataTable GetDataTable(string strQuery)
{
DataTable myTable = new DataTable();
try
{
myTable = GetDataSet(strQuery).Tables[0];
}
catch (Exception ex)
{
throw ex;
}
//返回数据集 myDataSet
return myTable;
}
public bool ExecuteSql(string strQuery)
{
bool flag = true;
OpenSql();
SqlTransaction myTra = con.BeginTransaction();
//创建操作数据库的命令
SqlCommand myCommand = new SqlCommand(strQuery, con, myTra);
try
{
//执行命令
myCommand.ExecuteNonQuery();
myTra.Commit();
}
catch (Exception ex)
{
flag = false;
myTra.Rollback();
throw ex;
}
CloseSql();
return flag;}
}
}