C # common strings used to connect to SQL Server

Source: Internet
Author: User

I. C # connect to the SQL database

DataSource = myServerAddress; Initial Catalog = myDataBase; UserId = myUsername; Password = myPassword;

DataSource = 190.190.200.100, 1433; Network Library = DBMSSOCN; InitialCatalog = myDataBase; User ID = myUsername; Password = myPassword;

Server = myServerAddress; Database = myDataBase; UserID = myUsername; Password = myPassword;

Trusted_Connection = False;

Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

Server = myServerName \ theInstanceName; Database = myDataBase; Trusted_Connection = True;

DataSource = myServerAddress; Initial Catalog = myDataBase; Integrated Security = SSPI;

1: Integrated Security parameters

When the value of Integrated Security is set to True, the user ID in front of the connection statement PW does not work, that is, the windows Authentication mode is used.

The connection is based on UserID and PW only when it is set to False or this item is omitted.

IntegratedSecurity can also be set to sspi, which is equivalent to True. We recommend that you use this instead of True.

DataSource = myServerAddress; Initial Catalog = myDataBase; Integrated Security = SSPI;

DataSource = myServerAddress; Initial Catalog = myDataBase; Integrated Security = true;

DataSource = myServerAddress; Initial Catalog = myDataBase; UserID = myUsername;

Password = myPasswordIntegrated Security = false;

2: Trusted_Connection

Trusted_Connection = true. The current Windows Account creden are used for authentication.

Trusted_Connection = false; the trusted connection method (that is, the Windows authentication method is not used) is not used, but the SQL Server 2000 authentication method is used.

Server = myServerAddress; Database = myDataBase; UserID = myUsername;

Password = myPassword; Trusted_Connection = false;

Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

3: Initial Catalog is the name of the database you want to connect

4: WINCE connection

DataSource = myServerAddress; Initial Catalog = myDataBase; Integrated Security = SSPI; UserID = myDomain \ myUsername; Password = myPassword;

Ii. You can use SqlConnectionStringBuilder to remember the name.

SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder ();

Scsb. DataSource = @ "(local) \ SQLExpress ";

Scsb. IntegratedSecurity = true;

Scsb. InitialCatalog = "Northwind ";

SqlConnectionmyConnection = new SqlConnection (scsb. ConnectionString );

3. You can use Setting in the property to automatically set the connection string.

1: Select (connection string) in type ),

2: select the data source from DataSouce, and then enter the Server name in the Server. Use local \ SQLExpress

3: select the login authentication method. This time, select Windows authentication (that is, trust the connection IntegratedSecurity = True)

4: select the Database Name and click OK.

DataSource = (local) \ SQLExpress; Initial Catalog = Northwind; Integrated Security = True

Server =. \ sqlexpress; integrated security = true; database = northwind

I have attached my own test case:

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Threading. Tasks;

Using System. Data;

Using System. Data. SqlClient;

Namespace ConnectDB

{

Class Program

{

Static void Main (string [] args)

{

SqlConnection sqlCon = new SqlConnection ();

SqlCon. ConnectionString = "Data Source = ServerName; Initial Catalog = DatabaseName; Integrated Security = SSPI ";

SqlCon. Open ();

SqlCommand SQL _Command = new SqlCommand ();

SQL _Command.CommandText = "Query Text ";

SQL _Command.Connection = sqlCon;

SqlDataAdapter dbAdapter = new SqlDataAdapter (SQL _Command );

DataSet dbSet = new DataSet ();

DbAdapter. Fill (dbSet );

// DbSet. GetXml ();

Console. WriteLine (dbSet. GetXml (). ToString ());

SqlCon. Close ();

Console. Read ();

}

}

}

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.