ASP.NET连接 Access数据库的几种方法
web.config
第一种方法:
<?xmlversion="1.0"encoding="utf-8"?> <!-- 有关如何配置ASP.NET应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <appSettings> <addkey="AccessConnString"value="provider=microsoft.jet.oledb.4.0;datasource="/> <addkey="AccessDbPath"value="~/App_Data/guestbook.mdb"/> </appSettings> <connectionStrings> <addname="AccessConnectionString"connectionString="Provider=Microsoft.Jet.OleDb.4.0;DataSource="providerName="System.Data.OleDb"/> <addname="Access_Path"connectionString="~/App_Data/guestbook.mdb"/> <addname="SqlConnectionString"connectionString="DataSource=localhost;InitialCatalog=guestbook;UserID=sa;password=123456;"providerName="System.Data.SqlClient"/> <addname="OracleConnectionString"connectionString="DataSource=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xp2008)(PORT=1521)))(CONNECT_DATA=(SID=guandb)(SERVER=DEDICATED)));UserID=system;Password=k2763826;Unicode=True"providerName="System.Data.OracleClient"/> </connectionStrings> <system.web> <compilationdebug="true"targetFramework="4.0"/> </system.web> </configuration>
第二种方法:
<?xmlversion="1.0"encoding="utf-8"?> <!-- 有关如何配置ASP.NET应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <appSettings> <addkey="AccessConnString"value="provider=microsoft.jet.oledb.4.0;datasource="/> <addkey="AccessDbPath"value="~/App_Data/guestbook.mdb"/> </appSettings> <connectionStrings> <addname="AccessConnectionString"connectionString="Provider=Microsoft.Jet.Oledb.4.0;datasource="/> <addname="Access_Path"connectionString="~/App_Data/guestbook.mdb"/> <addname="SqlConnectionString"connectionString="DataSource=localhost;InitialCatalog=HuaRunDb;UserID=sa;password=123456;"providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <compilationdebug="true"targetFramework="4.0"/> </system.web> </configuration>
后台aspx.CS
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.Configuration; usingSystem.Configuration; usingSystem.Data.OleDb; usingSystem.Data; namespaceTestOracle { publicpartialclassTestConnect:System.Web.UI.Page { publicstaticreadonlystringconnStr1=System.Web.Configuration.WebConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString+HttpContext.Current.Server.MapPath(WebConfigurationManager.ConnectionStrings["Access_Path"].ConnectionString); publicstaticreadonlystringconnStr2=System.Configuration.ConfigurationManager.AppSettings["AccessConnString"].ToString()+System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["AccessDbPath"])+";"; publicstaticreadonlystringconnStr3="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+HttpContext.Current.Server.MapPath("~/App_Data/guestbook.mdb"); protectedvoidPage_Load(objectsender,EventArgse) { } protectedvoidButton1_Click(objectsender,EventArgse) { OleDbConnectionconn=newOleDbConnection(connStr1); try { conn.Open(); stringsql="select*fromGtContent"; OleDbDataAdaptermyadapter=newOleDbDataAdapter(sql,conn); DataSetds=newDataSet(); myadapter.Fill(ds); this.GridView1.DataSource=ds; this.GridView1.DataBind(); this.Label1.Text="数据库连接成功!"; } catch(Exceptionee) { this.Label1.Text=ee.ToString(); } } } }
以上所述是小编给大家介绍的ASP.NET连接Access数据库的几种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!