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