方法一 用WEB.CONFIG 1.在WEN.CONFIG中加入如下代码: <appSettings> <add key ="oledbconacess" value="Provider=Microsoft.jet.oledb.4.0; data source={0}/App_data/bbb.mdb;User Id=admin;jet OleDB:Database Password=123"/> </appSettings> 2.在网页中加入如下代码: string connstr = string.Format(ConfigurationManager.AppSettings["oledbconacess"], HttpContext.Current.Server.MapPath(".")); OleDbConnection conn = new OleDbConnection(connstr); conn.Open(); string sqlstr = "SELECT * FROM stu "; OleDbDataAdapter da = new OleDbDataAdapter(sqlstr, conn);
DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; GridView1.DataSource = dt; GridView1.DataBind(); conn.Close(); 方法二 直接在网页中 string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;User Id=admin;jet OleDB:Database Password=123; Data Source=" + Server.MapPath("App_data\\bbb.mdb"); OleDbConnection conn = new OleDbConnection(connstr); conn.Open(); string sqlstr = "SELECT * FROM stu "; OleDbDataAdapter da = new OleDbDataAdapter(sqlstr, conn);
DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; GridView1.DataSource = dt; GridView1.DataBind(); conn.Close(); 方法三 使用datasource控件,不能用accessdatasource,要用SQLdatasource
本文来自CSDN博客,转载请标明出处:http://www.gao3.cn/asp/article/article48.htm
|