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.