ASP通过代码绑定Gridview控件

时间:2022-05-24 03:38:25

using System.Configuration;
using System.Data.OleDb;
using System.Data;

public partial class datafilm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str1 = ConfigurationManager.ConnectionStrings["accessconn"].ConnectionString.ToString(); //读取节点配置的字符
OleDbConnection connstr1 = new OleDbConnection(str1); //建立数据库连接对象
if (connstr1.State == System.Data.ConnectionState.Closed) //打开数据库
{
connstr1.Open();
}
string content = "select * from flim_info"; //编写SQL语句
OleDbDataAdapter oledba = new OleDbDataAdapter(content,connstr1);//建立oledbdataadapter对象
DataSet ds = new DataSet(); //建立dataset对象
oledba.Fill(ds); //填充数据集
GridView1.DataSource = ds; //设置GridView1的数据源
GridView1.DataBind(); //数据绑定
if (connstr1.State == System.Data.ConnectionState.Open) //关闭数据库
{
connstr1.Close();
}

}

}