ASP. NET connect SQL Server database and connect Access database Elite Edition

Source: Internet
Author: User
Tags access database

------------------------- Connect the access2003 string -------------------------

Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|\data.mdb
Connecting access2010 strings
Provider=microsoft.ace.oledb.12.0;data source=| Datadirectory|\aa.accdb
-------------------------theSQL database string -------------------------------
Data source= server; Initial catalog= database name; Integra Ted Security=true; /*windows Authentication */
server= server; User id= login name;   pwd= password; database= database name;              /*sqlserver Authentication */

Here's an example: see the Buckle 1.

1, the first to demonstrate the connection to the SQL database:

Using System.data.sqlclient;//sqlserver Database namespace

String connstr = "Data source=localhost;initial catalog=db_aspnet;  Trusted_connection=sspi; "; Establishing a Database connection channel
SqlConnection con = new SqlConnection (CONNSTR); Establishing a database Connection object

if (Con. state = = System.Data.ConnectionState.Closed)
{

Con. Open ();//Opening the database
Response.Write ("SQL Server database is turned on");
}
Label1.Text = con. State.tostring ();//For display in the UI, the same is not explained
if (Con. state = = System.Data.ConnectionState.Open)
{
Con. Close ();//Shut down the database
Response.Write ("SQL Server database is closed");

}
Label2.Text = con. State.tostring ();

2. Then connect to the Access database:

Using System.data.oledb;//access Database namespace

String connstr= "Provider=microsoft.ace.oledb.12.0;data source=| DATADIRECTORY|\\AA.ACCDB ";//Establish a database connection channel
OleDbConnection conn= New OleDbConnection (CONNSTR);//establishing a database connection object
IF (Conn. state = = System.Data.ConnectionState.Closed)
{

Conn. Open ();//Opening the database
Response.Write ("Access database is turned on");
}
Label1.Text = conn. State.tostring ();
IF (Conn. state = = System.Data.ConnectionState.Open)
{
Conn. Close ();//Shut down the database
Response.Write ("Access database is closed");

}
Label2.Text = conn. State.tostring ();

Do not like to spray, thank you!

ASP. NET connect SQL Server database and connect Access database Elite Edition

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.