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

Source: Internet
Author: User
Tags dsn mssql ole

MSSQL Connection string

(1) OLE DB
Windows Authentication

The code is as follows Copy Code
"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; ";

(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; database= database name; uid= user name; pwd= password;

(4) DSN method

The code is as follows Copy Code
DSN=DSN name; uid= user name; pwd= password;

Example: MySQL sample

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 OLE DB Access

OLE DB
How C # connects to access

Access 2003 provider=microsoft.jet.oledb.4.0
Access 2007 provider=microsoft.ace.oledb.12.0

           //(1) No database password, no user password, shared mode open
            //"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d: QJX document GENERICDEMODATASTUDENT.ACCDB; "
           //(2) has a database password, no user password, which is open exclusively
            //"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d: QJX document GENERICDEMODATASTUDENT.ACCDB; Jet oledb:database password=123 "
           //(3) There is a database password, there is a user password can use this method, but the user password even if a password must be empty, this way to open the exclusive
            //"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 OLE DB MSSQL

OLE DB
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 OLE DB Oracle
OLE DB
C # ways to connect 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 ();
}

}
}
}

home 1 2 last

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.