C # SQL server connection string description

Source: Internet
Author: User
Tags microsoft sql server 2005 sql server connection string sql server management studio

C # The sqlconnection string connecting to SQL Server, many of which cannot be connected. Now you can give a connection:

Using system. Data. sqlclient;
Public class sqlserverconnection

{
Public static void main ()

{

// The following is the standard connection. Note that the server is my computer name. Sqlexpress is a database instance. Sspi indicates the account used to log on to Windows. Master is the database to be connected.
//
String CSTR = "Server = server // sqlexpress; initial catalog = Master; Integrated Security = sspi ";
//
Try

{
Using (sqlconnection con = new sqlconnection (CSTR ))

{
Con. open ();
System. Console. writeline ("serverversion: {0}", Con. serverversion );
System. Console. writeline ("State: {0}", Con. State );
}

}
Catch (system. Exception sqle)

{
System. Console. writeline (sqle. Message );
}

}

}
/*
Program output:
Serverversion: 09.00.3042
State: Open
*/

// When no remote connection is used, change the connection string:
"Server = localhost // sqlexpress; Integrated Security = sspi"
// You can also specify the default database. For example, if the default database is "master", it should be written as follows:

"Server = localhost // sqlexpress; initial catalog = Master; Integrated Security = sspi"

Or: "Server = localhost // sqlexpress; initial catalog = Master; uid = sa; Pwd = sa"

B/S mode to connect to the database:
String SQL = "Source = Server IP address; database = database name; uid = sa; Password = Sasa ;";
Connect to the database in C/S Mode:
String SQL = "Data Source = Server IP address; initial catalog = database name; user id = sa; Password = sa ";

Connection to SQL Server 2005 Express

Error message:
"An error occurred while establishing a connection with the server. When you connect to SQL Server 2005, the default setting does not allow remote connection to SQL Server may cause this failure. (Provider: named pipeline provider, error: 40-unable to open the connection to SQL Server )"

Solution:

1. install an SQL Server Management studio Express. Microsoft official downloads, Baidu, and Google can be found.

2. After the installation is complete, find Microsoft SQL Server 2005 -- Configuration tool -- SQL Server peripheral application configurator -- service and connection peripheral application configurator in the Start Menu:
Open the database engine node under the MSSQLServer node, select "remote connection", and then select "use TCP/IP and named pipes at the same time". Then, restart the database service.

3. log on to SQL Server Management studio express in the default Windows Authentication mode, select the server in the list on the left, right-click to view properties, and select SQL Server and Windows Authentication Mode in "security ".

4. return to the main interface of SQL Server Management studio Express, create a login name for yourself under "security"-"Login Name", set it to SQL Server authentication, and then select the desired permissions on the left.

5. After completing the preceding four steps, use the following connection string to connect to SQL Server 2005.

"Data Source =./sqlexpress; initial catalog = Master; uid = sa; Pwd = sa"

Among them,./sqlexpress can write localhost/sqlexpress or machinename/localhost. Of course, the first method is the easiest and suitable for local machines.

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.