# Two code connection strings used to connect to SQLServer: & lt; connectionStrings & gt; & lt; addname & quot; conn & quot; connectionString & quot; useridsa; Password; initialcatalogNorthwind; ServerYourSQLServer; connectTimeou # code for connecting to SQL Server
Connection string:
--------------------------------------------------------------------------------
Description of parameters (Note: parameters are separated by semicolons ):
"User id = sa": the connection authentication username is sa. It also has an alias "uid", so we can also write this statement as "uid = sa ".
"Password =": The verification password for connecting to the database is blank. Its alias is "pwd", so we can write it as "pwd = ".
Note that your SQL Server must have a user name and password set to log on. Otherwise, you cannot log on using this method. if your SQL Server is set to Windows logon, you do not need to use the "user id" and "password" methods to log on here, you need to use "Trusted_Connection = SSPI" to log on.
"Initial catalog = Northwind": the data source used is "Northwind". Its alias is "Database". This statement can be written as "Database = Northwind ".
"Server = YourSQLServer": Use a Server named "YourSQLServer. its alias is "Data Source", "Address", "Addr ". if the local database is used and the Instance name is defined, you can enter "Server = (local) Instance name". If it is a remote Server) "Replace with the name or IP address of the remote server.
"Connect Timeout = 30": the connection Timeout is 30 seconds.
Note: The above User ID and Password can be in upper or lower case, regardless of case.
C # connect to SQL Server
Program code:
Using System. Data;
Using System. Data. SqlClient; // namespace used
String connectionString = "Server (Data Source) = Server name; initial catalog (Database) = Database name; user id (uid) = user name; password (pwd) = password; (Windows login mode: Trusted Connection = SSPI) Connect Timeout = 30 ";
SqlConnection connectionObj = new SqlConnection (connectionString); // create a connection object
ConnectionObj. Open ();
ConnectionObj. Close ();
Explanation:
"User id = sa": the authenticated user name for database connection is sa. He also has an alias "uid", so we can also write this sentence as "uid = sa ".
"Password =": The verification password for connecting to the database is blank. Its alias is "pwd", so we can write it as "pwd = ".
Note that your SQL Server must have a user name and password set to log on. Otherwise, you cannot log on using this method. If your SQL Server is set to Windows logon, you do not need to use the "user id" and "password" methods to log on here, you need to use "Trusted_Connection = SSPI" to log on.
"Initial catalog = Northwind": the data source used is "Northwind. Its alias is "Database". This sentence can be written as "Database = Northwind ".
"Server = YourSQLServer": Use a Server named "YourSQLServer. Its alias is "Data Source ",
"Address", "Addr ". If the local database is used and the Instance name is defined, you can enter "Server = (local) Instance name". If it is a remote Server) "With the name or IP address of the remote server.
"Connect Timeout = 30": the connection Timeout time is 30 seconds.
Here, the constructor used to create a connection object is SqlConnection.