C # two codes for connecting to SQLServer

Source: Internet
Author: User
# 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.

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.