asp.net connection Access database path problem

Source: Internet
Author: User
Tags config database join httpcontext access database

Solution One:

Configure the Access database tutorial driver and database file name in Web.config.
Please look at the code

<appSettings>
<add key= "Dbdriver" value= "provider=microsoft.jet.oledb.4.0; Data Source = "/>
<add key= "dbname" value= "Company.mdb"/>
</appSettings>

Get the Access database link string in the database access layer, such as OleDBHelper.cs.

/**////<summary>
To obtain a database join string from web.config
</summary>
To get the database name from the configuration file
public static readonly String dbname = ConfigurationManager.AppSettings.Get ("dbname"). ToString ();
Get database driver from configuration file
public static readonly String dbdriver = ConfigurationManager.AppSettings.Get ("Dbdriver"). ToString ();
Get the database connection string
private static string dbconnectionstring = Dbdriver + HttpContext.Current.Server.MapPath ( HttpContext.Current.Request.ApplicationPath + "/app_data/") + dbname;
To establish a database connection object
private static OleDbConnection oledbconn = new OleDbConnection (dbconnectionstring);

After this setting, the database can be accessed correctly by the above code, regardless of any subdirectories.


Solution Two:

<appSettings>
<add key= "sqlconnstring" value= "Provider=Microsoft.Jet.OLEDB.4.0;Data source="/>
<add key= "DBPath" value= "~/app_data/mydata.mdb"/>
</appSettings>

In the data access class in the program I take "sqlconnstring" and "DBPath" out to connect to a string "CONN_STRING_NON_DTC"

public
static readonly string CONN_STRING_NON_DTC = system.configuration.configurationmanager.appsettings[" Sqlconnstring "]. ToString () + System.Web.HttpContext.Current.Server.MapPath (configurationmanager.appsettings["DBPath"]) + ";";
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.