C # Two kinds of code to connect SQL Server

Source: Internet
Author: User
Tags connectionstrings

Two types of code #连接SQL server


Connection string:

<connectionStrings>

<add name= "conn" connectionstring= "User Id=sa; Password=;initial Catalog=northwind; Server=yoursqlserver; Connect timeout=30; "providername=" System.Data.SqlClient "/>

</connectionStrings>

--------------------------------------------------------------------------------

Introduction to parameters (Note: Separating parameters by semicolons):

User id=sa: The authentication user name for the connection database tutorial is SA. He also has an alias "UID", so we can also write "Uid=sa".

"Password=": The authentication password for the connection database is empty. His alias is "pwd", so we can write it as "pwd=".

Notice here that your SQL Server must have been set up with a username and password to log in, otherwise you won't be able to log in this way. If your SQL Server is set up for Windows login, you don't need to use the user ID and password here. This way to log in, and you need to use "TRUSTED_CONNECTION=SSPI" to log in.

"Initial Catalog=northwind": The data source used is the "Northwind" database. His alias is "database", this sentence can be written as "Database=northwind".

"Server=yoursqlserver": Use a server named "YourSQLServer". His alias is "Data Source", "Address", "Addr". If you are using a local database and you have defined an instance name, you can write as " server= (local) instance name, or if it is a remote server, replace "(local)" with the name or IP address of the remote server.

"Connect timeout=30": Connection timeout is 30 seconds.

Note: The above user id,password can be uppercase or lowercase, regardless of case


C # connecting SQL Server
Program code:

Using System.Data;

Using System.Data.SqlClient; namespaces to use

String connectionstring= "server (Data Source) = server name; Initial Catalog (db) = database name; user ID (UID) = user name; password (pwd) = password ; (Windows login mode: Trusted connection=sspi) Connect timeout=30 ";

SqlConnection connectionobj=new SqlConnection (ConnectionString); To establish a Connection object

Connectionobj.open ();

Connectionobj.close ();

Explain:

' User Id=sa ': The authentication user name for the connection database is SA. He also has an alias "UID", so we can also write "Uid=sa".

"Password=": The authentication password for the connection database is empty. His alias is "pwd", so we can write it as "pwd=".

Note here that your SQL Server must have been set up with a username and password to log in, otherwise you won't be able to log in this way. If your SQL Server is set up for Windows logon, you do not need to use the "User ID" and "password" method to log in, and you need to use "TRUSTED_CONNECTION=SSPI" to log in.

"Initial Catalog=northwind": The data source used is the "Northwind" database. His alias is "Database", this sentence can be written as "Database=northwind".

"Server=yoursqlserver": Use a server named "YourSQLServer". His alias is "Data Source,"

"Address", "Addr". If you are using a local database and the instance name is defined, you can write as "server=" or, if it is a remote server, replace "(local)" with the name or IP address of the remote server.
"Connect timeout=30": Connection timeout is 30 seconds.


Here, the constructor used to establish the connection object is: SqlConnection.

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.