C # Common database connection string "Go"

Source: Internet
Author: User
Tags mssqlserver server port

One: C # connection to SQL database

Data source=myserveraddress;initial catalog=mydatabase; User Id=myusername; Password=mypassword;
Data source=190.190.200.100,1433; Network library=dbmssocn;initial catalog=mydatabase; User Id=myusername; Password=mypassword;
Server=myserveraddress;database=mydatabase; User Id=myusername; Password=mypassword; Trusted_connection=false;
Server=myserveraddress;database=mydatabase; Trusted_connection=true;
Server=myservername\theinstancename;database=mydatabase; Trusted_connection=true;
Data source=myserveraddress;initial catalog=mydatabase;integrated Security=sspi;

1:integrated Security Parameters
When setting integrated security to True, the UserID in front of the connection statement is not functional, that is, the Windows Authentication mode is used.
Only if the item is set to False or omitted, the connection is followed by the UserID, PW.
Integrated Security can also be set to: SSPI, equivalent to true, it is recommended to use this instead of true.
Data source=myserveraddress;initial catalog=mydatabase;integrated Security=sspi;
Data source=myserveraddress;initial catalog=mydatabase;integrated security=true;
Data source=myserveraddress;initial catalog=mydatabase;; User Id=myusername; Password=mypasswordintegrated Security=false;

2: Parameter Trusted_Connection
Trusted_connection=true, the current Windows account credentials will be used for authentication
Trusted_connection=false: The Trusted connection method (that is, no Windows authentication) will be used instead of the SQL Server 2000 authentication method
Server=myserveraddress;database=mydatabase; User Id=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 to

4:wince Connection
Data source=myserveraddress;initial catalog=mydatabase;integrated Security=sspi; User Id=mydomain\myusername; Password=mypassword;

Two: You can use SqlConnectionStringBuilder, so you don't have to remember the name.
SqlConnectionStringBuilder SCSB = new SqlConnectionStringBuilder ();
SCSB. DataSource = @ "(local) \SQLExpress";
SCSB. IntegratedSecurity = true;
SCSB. InitialCatalog = "Northwind";
SqlConnection myconnection = new SqlConnection (SCSB. ConnectionString);

Three: You can use the setting in the property to set the connection string
1: Select in type (connection string),
2: Select the data source in the Datasouce, and then enter the server name, locally (local) \SQLExpress
3: Choose Login authentication method, this time select Windows authentication (that is, trust connection integrated security=true)
4: Select the database name, confirm
Data source= (local) \sqlexpress;initial catalog=northwind;integrated security=true
Server =. \sqlexpress;integrated Security = True;database = Northwind

Four: SQL2005 remote server connection method

How to open 1433 port for SQL Server 2005:
Configure the tool->sql Server configuration manager->mssqlserver the protocol to see if the TCP/IP protocol starts, if you start, right-click the menu Point "Properties", select "IP Address" in the paging menu, "IP1" and " IP2 "TCP port" is 1433, "enabled" changed to "yes"
Configuration Tool->sql Server configuration manager->sql Native client-side protocol->TCP/IP Select Properties in the TCP/IP right-click menu, confirm "Default port" Is 1433, "enabled" is "yes".

SQL Server 2005 Remote Connection Configuration TCP/IP properties:
Surface Area Configuration----and Remote Connections--->using tcp/it SQL Server surface Area Configuration? service and Connectivity Perimeter Configuration. Database Englie remote connection enabled (remote connection TCP/IP and Named pipes)
SQL Server Configuration Manager? SQL2005 network configuration? Enable TCP/IP and Named pipes

Other instructions see below: sqlserver2005 (Express Edition), for ease of management, you also need to go to the next manage manager:
After installing the Manage manager, connect the sqlserver2005 in the program, the following points are to be noted.
1. Turn on the SQL2005 remote connection function, open the method as follows, configure the tool->sql server surface Area Configuration, services and connections, and connect to the external configuration of the database Engine node under the MSSQLServer node, select the " Remote Connection ", followed by the recommendation to select" Use both TCP/IP and Named pipes ", after you determine, restart the database service.
2. Login settings to, SQL server and Windows Authentication mode is selected, the following settings: Manage Manager->windows authentication (first time in Windows),- > select your data server in Object Explorer--right-click > Properties >security>sql Server and Windows Authentication mode are selected simultaneously.
3: Set up a SQL Server user name and password as follows: Manage manager->windows authentication>new query>sp_password null, ' sa123456 ', ' Sa ' This set a user named SA, the password is: sa123456 users, the next time you log in, you can use SQL Server mode, the user name is SA, password is: sa123456 user into the database.
4: Finish the above three steps, so write the connection string will be able to enter the database smoothly,
(Server=.\sqlexpress;uid=sa;pwd=sa123456;database=master ";

V: SQL2000 Remote Server connection method

1: See if the ping server IP can ping.
2: Enter the Telnet server IP port under DOS or command line to see if it can be connected. For example, Telnet 202.114.100.100 1433 typically has a port value of 1433, because 1433 is the default listening port for TCP/IP for SQL Server 2000. If there is a problem, this step usually goes wrong. The usual hint is "... Unable to open connection, connection failed. "
If there is a problem with this step, you should check the following options.
1) Check whether the remote server has started the SQL Server 2000 service. If not, it starts.
2) Check that the TCP/IP protocol is not enabled on the server side because remote connections (via the Internet) require this protocol. The check method is to open the Microsoft SQL server-> Server Network Utility, Start menu, programs, on the server, to see if there is a TCP/IP protocol in the enabled Protocol, and if not, enable it.
3) Check that the TCP/IP port of the server is configured as 1433 port.   In the Server Network Utility, check the TCP/IP properties in the Enable protocol, make sure the default port is 1433, and the Hide server check box is not checked. In fact, if the default port is modified, it is also possible, but when the client does the Telnet test, the server port number must be the same as the port number configured by the server. If the Hide Server check box is checked, it means that the client cannot enumerate the server to see this server, but does not affect the connection, but the TCP/IP protocol default port will be implicitly modified to 2433, when the client connection must be changed accordingly.
4) If the server-side operating system has SP2 patch, you have to configure the Windows Firewall, to open 1433 ports to it, usually in the test can be directly shut down Windows Firewall (the other firewall is also turned off the best).
5) Check if the server is listening on port 1433. If the server is not listening on port 1433 on a TCP connection, it is not connected. The check method is to enter Netstat-a-N or Netstat-an under the DOS or command line of the server, and see if there are items like TCP 127.0.0.1 1433 listening in the results list. If not, you will typically need to make at least SP3 patches to SQL Server 2000.  In fact, start the Query Analyzer on the server side, enter the SELECT @ @version can see the version number, the version number in 8.0.2039 or less needs to be patched. If the above is not a problem, then you do telnet server IP 1433 test, you will see the screen flash after the cursor is flashing in the upper left corner. Congratulations, you can start right away. The Enterprise Manager or Query Analyzer is connected.

3: Check the client setup program, Microsoft SQL Server, Client network usage tool. As in the server Network Utility, make sure that the client TCP/IP protocol is enabled and that the default port is 1433 (or another port that is consistent with the server side).
4: In Enterprise Manager or query that Analyzer connection Test Enterprise Manager, right-click SQL Server Group, new SQL Server Registration--next--write remote ip-> Next, click SQL Server Login Next, write the login name and password (sa,password), and then complete the Query Analyzer----FILE--write remote ip-> write logins and Passwords (Sa,password)-& Gt  It is generally recommended to do this in Query Analyzer because by default the timeout setting for registering another SQL Server through Enterprise Manager is 4 seconds, and the Query Analyzer is 15 seconds. To modify the default connection time-out: In the SQL Server Enterprise Manager Properties window that pops up with options, tools, Enterprise Manager, click Enter a larger numeric query in the box after login timeout (seconds), Tap Advanced tab, connection settings Connection, tools-----------The box after login timeout (seconds), enter a larger number that is usually connected and, if prompted incorrectly, go to the next step.
5: The cause of the error is usually because SQL Server uses Windows-only authentication, so users cannot connect using SQL Server's login account (such as SA). The workaround is as follows:
1) Use Enterprise Manager on the server side, and select "Use Windows Authentication" to connect to SQL Server.
2) Expand SQL Server Group, right-click the name of your SQL Server server, select Properties, and then select the Security tab.
3) Under Authentication, select SQL Server and Windows.
4) Restart the SQL Server service. (under DOS or command line, net stop MSSQLSERVER stop service, net start MSSQLServer start service, is also a quick method).


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/fredrickhu/archive/2009/12/08/4961799.aspx

C # Common database connection string "Go"

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.