C # Common strings for SQL Server connection

Source: Internet
Author: User

One: C # connect to 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=myservernametheinstancename;database=mydatabase; Trusted_connection=true;

Datasource=myserveraddress;initial catalog=mydatabase;integrated Security=sspi;

1:integrated Security Parameters

When the integrated security is set to True, the userid in front of the connection statement, PW, does not work, that is, the Windows Authentication mode is used.

Only set to False or omit the item, and then follow UserID, PW to connect.

IntegratedSecurity can also be set to: SSPI, which is the equivalent of true, which is recommended 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: Parameter Trusted_Connection

Trusted_connection=true, authentication is performed using the current Windows account credentials

Trusted_connection=false will not take a trusted connection (that is, not Windows authentication), instead of the SQL Server 2000 authentication method

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 to.

4:wince Connection

Datasource=myserveraddress;initial catalog=mydatabase;integrated Security=sspi; Userid=mydomainmyusername; Password=mypassword;

Two: You can use sqlconnectionstringbuilder so that you don't have to remember the name.

SqlConnectionStringBuilder SCSB = new SqlConnectionStringBuilder ();

SCSB. DataSource =@ "(local) SQLExpress";

SCSB. IntegratedSecurity = true;

SCSB. InitialCatalog = "Northwind";

Sqlconnectionmyconnection = new SqlConnection (SCSB. ConnectionString);

Three: You can use the setting from the property to set the connection string

1: Select in type (connection string),

2: Select the data source in Datasouce, and then enter the server name in server, locally (local) SQLExpress

3: Choose login Authentication Mode, this time select Windows authentication (that is, trust connection integratedsecurity=true)

4: Select the database name, confirm can

Datasource= (local) sqlexpress;initial catalog=northwind;integrated security=true

Server =.sqlexpress;integrated Security = True;database = Northwind

Finally, attach a test case of my own:

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.