求教一个数据绑定的问题

来源:百度知道 编辑:UC知道 时间:2024/05/03 08:50:34
private void databind()
{

SqlConnection connection = db.createconnection();
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter();

sda.SelectCommand = new SqlCommand("select * from lyb order by id desc", connection);
DataSet ds = new DataSet();
sda.Fill(ds, "lyb");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["lyb"].DefaultView;
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
connection.Close();
}

你的项目中有db这个类么?这个就是你用的db.createconnection();
你需要修改为

类名 db=new 类名();
SqlConnection connection = db.createconnection();

…………

如果没有你可以修改为:

private void databind()
{
SqlConnection connection = new SqlConnection("server=127.0.0.1;uid=sa;pwd=;database=数据库名;");
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter();

sda.SelectCommand = new SqlCommand("select * from lyb order by id desc", connection);
DataSet ds = new DataSet();
sda.Fill(ds, "lyb");
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["lyb"].DefaultView;
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
connection.Close();
}