Connecting various database string connection programs in asp.net (mysql/mssql/access) (1/2)

Source: Internet
Author: User

I have added my favorite database string connection programs, including mysql, mssql, and access. You can also send other programs.

MSSQL connection string

(1) OLEDB
Windows Authentication

The Code is as follows: Copy code
& Quot; Provider = SQLOLEDB; Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes; & quot ;";
SQL Server Authentication
"Provider = SQLOLEDB; Data Source = 127.0.0.1SQLEXPRESS; Initial Catalog = Test; User Id = sa; Password = 123456 ;";

(2) SqlConnection
Windows Authentication

The Code is as follows: Copy code
"Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;";
SQL Server Authentication
"Server = 127.0.0.1SQLEXPRESS; database = Test; Uid = sa; PassWord = 123456 ";

(3) ODBC

The Code is as follows: Copy code
Driver = {SQL Server}; Server = Server IP address; Database = Database name; Uid = user name; Pwd = password;

(4) DSN Mode

The Code is as follows: Copy code
DSN = DSN name; Uid = user name; Pwd = password;

 

Example: mysql example

The Code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Data;
Using System. Data. Common;
Using System. Linq;
Using System. Text;
Using System. Data. OleDb;
Using System. Data. SqlClient;
Using System. Data. SQL;

Namespace ado
{
Public class ConnectionDemo1
{
Public void GetOleDbTable ()
{
# Region namespace
// Using System. Data. OleDb;
# Endregion

# Region OLEDB Access

// OLEDB
// C # Access method

// Access 2003 Provider = Microsoft. Jet. OLEDB.4.0
// Access 2007 Provider = Microsoft. Ace. OleDb.12.0

// (1) No Database Password, no user password, open in sharing mode
// "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb ;"
// (2) there is a database password and no user password. This mode is enabled exclusively.
// "Provider = Microsoft. Jet. OleDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 123"
// (3) You can use this method if you have a database password. However, even if you have a password, it must be empty. This mode is enabled exclusively.
// "Provider = Microsoft. jet. oleDB.4.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 12345; Persist Security Info = true; password =; user id = Admin"

# Endregion

# Region OLEDB MSSQL

// OLEDB
// C # how to connect to MSSQL

// Windows Authentication
// "Provider = SQLOLEDB; Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;";
// SQL Server Authentication
// "Provider = SQLOLEDB; Data Source = 127.0.0.1SQLEXPRESS; Initial Catalog = Test; User Id = sa; Password = 123456 ;";

# Endregion

# Region OLEDB Oracle
// OLEDB
// C # connection to Oracle

// "Provider = MSDAORA; Data Source = MyDatabaseAlias; User ID = MyUID; Password = MyPassword ";
# Endregion

// String strConn = @ "Provider = Microsoft. Ace. OleDb.12.0; Data Source = D: qjx document GenericDemoDataStudent. accdb; Jet OleDb: DataBase Password = 123"; // Access 2007
String strConn =
@ "Provider = SQLOLEDB; Data Source = 127.0.0.1SQLEXPRESS; Initial Catalog = Test; User Id = sa; Password = 123456 ;";
OleDbConnection conn = new OleDbConnection (strConn );
OleDbCommand comm = new OleDbCommand ();

Comm. CommandText = "SELECT * FROM Users ";
Comm. CommandType = CommandType. Text;
Comm. Connection = conn;
If (conn. State! = ConnectionState. Open)
{
Conn. Open ();
DataSet ds = new DataSet ();
OleDbDataAdapter da = new OleDbDataAdapter (comm );
Da. Fill (ds );
Conn. Close ();
}
}

Public void GetSqlConnectionTable ()
{
# Region namespace
// Using System. Data. SqlClient;
# Endregion

# Region MSSQL SqlConnection
// SqlConnection
// C # how to connect to MSSQL

// Windows Authentication
// "Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;";
// SQL Server Authentication
// "Server = 127.0.0.1SQLEXPRESS; database = Test; Uid = sa; PassWord = 123456 ";
# Endregion

// String strConn = @ "server = 127.0.0.1SQLEXPRESS; database = Test; Uid = sa; PassWord = 123456 ";
String strConn = @ "Data Source = PEK7-6TKX23XSQLEXPRESS; Initial Catalog = Test; Trusted_Connection = Yes ;";

SqlConnection conn = new SqlConnection (strConn );
SqlCommand comm = new SqlCommand ();
Comm. CommandText = "SELECT * FROM Users ";
Comm. CommandType = CommandType. Text;
Comm. Connection = conn;

If (conn. State! = ConnectionState. Open)
{
Conn. Open ();
DataSet ds = new DataSet ();
SqlDataAdapter da = new SqlDataAdapter (comm );
Da. Fill (ds );
Conn. Close ();
}

}
}
}

 

1 2

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.