外部访问DATASET

来源:百度知道 编辑:UC知道 时间:2024/05/22 20:28:53
在事件中如何访问方法中的定义好的DATASET
protected void Button1_Click(object sender, EventArgs e)
{ 如何访问DATASET
}
public void getdata()//获取GRIDVIEW数据
{
SqlConnection conn=new SqlConnection ("Data Source=.;Initial Catalog=fcc;Persist Security Info=True;User ID=sa;Password=ffff");
SqlCommand cmd=new SqlCommand ("select danwei_name,wangdian_name,host_sn,host_ip from terminal",conn);
DataSet ds=new DataSet ();
SqlDataAdapter da=new SqlDataAdapter (cmd);
da.Fill (ds);
this.GridView1 .DataSource =ds.Tables [0].DefaultView ;
this.GridView1 .DataBind ();
}

protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds=getdata();
this.GridView1 .DataSource =ds.Tables [0];
this.GridView1 .DataBind ();

}
public DataSet getdata()//获取DataSet
{
SqlConnection conn=new SqlConnection ("Data Source=.;Initial Catalog=fcc;Persist Security Info=True;User ID=sa;Password=ffff");
SqlCommand cmd=new SqlCommand ("select danwei_name,wangdian_name,host_sn,host_ip from terminal",conn);
DataSet ds=new DataSet ();
SqlDataAdapter da=new SqlDataAdapter (cmd);
da.Fill (ds);

return ds;
}

建议这么写,把读取数据,和与数据库绑定部分分离开。
getdata()方法返回类型设置为DataSet ,在别的方法中可以直接调用这个DataSet了