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 ();
}
}
}