Optimal Method for connecting ASP. NET to access database web. config

Source: Internet
Author: User

A lot of friends have been accessed by the database in. netProgramThe problem of relative paths in, so every time a mobile program wants to modify web. config
Database path of the database connection string in.

The Web. config statement is as follows:

<Deleetask>
<Add key = "oledbconnectionstring" value = "provider = Microsoft. Jet. oledb.4.0; Data
Source = E: \ WEB \ app_data \ data. mdb) "> </Add>
</Appsettings>

Write in the program as follows:

Myconn = new oledbconnection (system. configuration. configurationmanager. deleetpipeline ["oledbconnectionstring"]); // Note: The configurationsettings statements in vs2005 and vs2003 are different. Check the differences.

In this way, errors such as the following are often prompted during the program running:
'C: \ windows \ system32 \~ \ App_data \ data. mdb 'is not a valid path. Check whether the path name is correctly spelled and whether it is connected to the server where the file is stored. Data Source = ~ \ App_data \ data. MDB
Even if the absolute path is correct, you need to modify web. config when porting the program, which is troublesome.

Some web. config also uses server. mappath like ASP to retrieve the database path. However, Web. config does not know server. mappath, and this method does not work.

Later, by exploring and referring to other programs, we summarized the following methods to facilitate program migration without having to modify the ACCESS database path.

My web. config statement is as follows:

<Deleetask>
<Add key = "sqlconnstring" value = "provider = Microsoft. Jet. oledb.4.0; Data Source ="/>
<Add key = "dbpath" value = "~ /App_data/mydata. mdb "/>
</Appsettings>
In the program's data category class, I took "sqlconnstring" and "dbpath" and connected them 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"]) + ";";

End of ASP. NET optimal way to connect to the ACCESS database web. config

Post 2 web. config configure the relative path of access

I am using VS 2005. After debugging, how can I implement it,CodeAs follows (VB. NET example, C # method is the same, not to mention here)

First, add the following code to the <etettings> node of the web. config file:
<! -- Data source -->
<Add key = "myds" value = "provider = Microsoft. Jet. oledb.4.0; Data Source ="/>
<! -- Database relative path -->
<Add key = "myconn" value = "app_data \ vinikedata. mdb"/>

Then, write a class file and call the above definition. The Code is as follows:

'The difficulty lies in defining a connection string. Many servers are used online. mappath, but it still does not work. Note that the request is used here. mappath (this is useless to ASP)
Public connstr as string = configurationsettings. receivettings ("myds") + httpcontext. Current. Request. mappath ("~ ") + (Configurationsettings. receivettings (" myconn "). Trim ())

 

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb;
/// <Summary>
/// Summary of Connection
/// </Summary>
Public class connection
{
Public connection ()
{
//
// Todo: add the constructor logic here
//
}
Public static oledbconnection connaccess ()
{
Oledbconnection conn = new oledbconnection (getconnstring ());
Return conn;
}
Private Static string getconnstring ()
{
Return System. configuration. configurationsettings. receivettings ["connstr"] + system. Web. httpcontext. Current. server. mappath ("~ ") + System. configuration. configurationsettings. receivettings [" dbpath "];
}

}

 

 

using system;
using system. data;
using system. configuration;
using system. collections;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;
using system. data. oledb;
Public partial class test_sss_defauldddt: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{

}
Protected void button#click (Object sender, eventargs E)
{
System. Data. oledb. oledbconnection conn1 = connection. connaccess ();
Conn1.open ();
If (conn1.state = connectionstate. open)
{
Label1.text = "connection successful! ";
}
Else
{
Label1.text = "connection failed! ";
}
}
}

Post 3 C #2.0 web. config

<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">
<Appsettings/>
<Connectionstrings>
<Add name = "book" connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source = D: \ C # \ book \ app_data \ book. mdb"
Providername = "system. Data. oledb"/>
</Connectionstrings>

 

Database Calling:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
// The namespace must be introduced here because you need to use the access database.
Using system. Data. oledb;

/// <Summary>
/// This is mainly the database connection class used in the message book. because there are not many data operations, the conventional operation classes are also put here.
/// </Summary>
Public class ODB
{
Public string name;
Public String email;
Public String QQ;
Public String MSN;
Public String URL;
Public String title;
Public String concent;
Public String face;
Public String IP;
Public String PWD;
Public String uid;
Public String pwda;
Public datetime DTT;

Public ODB ()
{
//
// Todo: add the constructor logic here
//
}
Public static oledbconnection con ()
{// Database connection class
Oledbconnection con = new oledbconnection (configurationmanager. connectionstrings ["book"]. connectionstring );
Return con;
}
Public static bool insert (string que)
{// Insert, delete, update, and other operations based on the passed SQL statement
Oledbconnection con = ODB. Con ();
Con. open ();
Oledbcommand cmd = new oledbcommand (que, con );
Int COUNT = convert. toint32 (CMD. executenonquery ());
If (count> 0)
Return true;
Else
Return false;
}
Public static datatable DS (string que)
{// Return a data table loaded with an SQL-based message,
Oledbconnection con = ODB. Con ();
Oledbdataadapter ODA = new oledbdataadapter ();
ODA. selectcommand = new oledbcommand (que, con );
Dataset DS = new dataset ();
ODA. Fill (DS, "thc ");
Return Ds. Tables ["thc"];
}
Public static bool IMG (string que)
{// Query whether the specified item has content based on the sent condition. If yes, true is returned.
Oledbconnection con = ODB. Con ();
Con. open ();
Oledbcommand cmd = new oledbcommand (que, con );
If (CMD. executescalar (). tostring ()! = "")
Return true;
Else
Return false;
Con. Close ();
}
Public static string SCR (string que)
{// Return the value of a field based on the sent SQL statement. Generally, the SQL statement should be implemented in the class.
Oledbconnection con = ODB. Con ();
Con. open ();
Oledbcommand cmd = new oledbcommand (que, con );
Return cmd. executescalar (). tostring ();
}
Public static int num (string mm)
{// Return the number of entries per entry as required
Return convert. toint32 (ODB. scr ("select [" + mm + "] from [config]");
}
}
Post 4How to connect to the ACCESS database on the Asp.net logon interface for verification
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb;
Public partial class login: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// The following code generates a random Verification Code and is displayed in label1.
Random RO = new random ();
If (! Ispostback)
{
This. label1.text = Ro. Next (1000,999 9). tostring ();
}
}
Protected void button#click (Object sender, eventargs E)
{

If (this. Name. Text! = "") // Determines whether the user name is empty
{
If (this. pwd. Text! = "") // Determines whether the password is empty
{
If (this. yanzhen1.text! = "") // Check whether the verification code is empty
{
If (this. yanzhen1.text = This. label1.text) // checks whether the verification code is equal.
{
String SQL;
SQL = "select count (*) from userinfo where username = '" + this. name. text + "'and Pwd ='" + this. PWD. text + "'"; // create an SQL query statement
Try
{
Oledbconnection conn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath (". /app_data/DB. mdb "); // create a database connection
Conn. open ();
Oledbcommand cmd = new oledbcommand (SQL, Conn );
Int state = convert. toint32 (CMD. executescalar (); // execute the SQL statement and return the obtained value.
If (State = 0 | State> 1) // if there are no or multiple records in the data, an error is returned.
{
This. label2.text = "the user does not exist. Check whether the user name and password are correct! ";
}
Else
{
This. label2.text = "Login successful! ";

}
Conn. Close ();

}
Catch (Exception)
{
Response. writea. Message );
}
}
Else
{
This. label2.text = "the verification code is incorrect. Please enter it again! ";
}
}
Else
{
This. label2.text = "the verification code is not filled! ";

}
}
Else
{
This. label2.text = "the password is not entered! ";

}
}
Else
{
This. label2.text = "the user name is not filled! ";
}
}
}

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.