C # connect to SQL database

Source: Internet
Author: User
Tags microsoft sql server mssqlserver server port

A: C # connection 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 set Integrated security is True, the UserID in front of the connection statement is not functional, that is, the Windows Authentication mode is used.      Only when set to False or omit this entry, follow the UserID, PW to connect.      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: Parameters trusted_connection     Trusted_connection=true, authentication with current   windows  account credentials     Trust Ed_connection=false: The trusted connection method (i.e., no Windows authentication) will be used instead of the SQL Server 2000 authentication method     SERVER=MYSERVERADDRESS;D Atabase=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;integrat Ed 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 from the properties to set the connection string     1: Select in type (connection string),     2: Select the data source in Datasouce, Then enter the server name in the server, local \sqlexpress     3: Choose Login authentication method, this time select Windows authentication (that is, trust Connection integrated security=true)   & nbsp 4: Select database name, confirm     Data source= (local) \sqlexpress;initial catalog=northwind;integrated security=true   & nbsp Server =. \sqlexpress;integrated Security = True;database = Northwind Four: SQL2005 remote server connection method how to open 1433 ports for SQL Server 2005 : Configure the tool->sql Server configuration manager->mssqlserver the protocol to see if the TCP/IP protocol starts, if it starts, right-click the menu Point "Properties", select "IP Address" in the paging menu, "IP1" and " IP2 "TCP Port" is 1433, "enabled" is changed to "Yes" configuration tool->sql Server configuration manager->sql Native Client Configuration--Clients 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------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?s QL2005 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,   In the program to connect the sqlserver2005, 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 ";   Five: SQL2000 remote Server connection Method 1: See if the ping server IP can ping. 2: In Dos or the command line to enter the Telnet server IP port 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 Windows Firewall to open 1433 ports to it, usually you can switch off the Windows Firewall (the other firewall is also turned off the best) when testing.     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. Actually start the query on the server sideParser, enter SELECT @ @version can see the version number after execution, the version number under 8.0.2039 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 on SQL Server.     2) Expand SQL Server Group, right-clickName of the 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).

C # Connect to a SQL database

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.