Using Visual Studio 2005 and SQL Server 2005 to create a database connection



In Visual Studio 2005 to connect SQL Server 2000, we may know, the connection string will be relatively simple, generally when the remote connection, only in the Web.config file configuration is as follows:



Then in the code by calling the following statements can be achieved connected database.

SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings ["ConnectionString"]);

Under this premise just need to set up a SQL Server Authentication and Windows authentication on it.

In SQL Server 2005, if such action would be reported directly to the following error:

In establishing connection to the server error. In the Connect to SQL Server 2005 when the default settings SQL Server does not allow remote connection may cause this failure. (Provider: Named Pipes Provider, error: 40 - Could not open connection to SQL Server).

We need a separate configuration of the SQL Server 2005, follow these steps:

1, configure SQL Server external application server, open the SQL2005 remote connection features:

Operation as follows, click "Configuration Tools" -> "SQL Server Surface Area Configuration", then open windows in select "services and connections Surface Area Configuration" -> then select the Database Engine node in the "Remote connection ", select" Local Area Connection and remote connections ", and select" use both TCP / IP and named pipes ", confirmed, and then need to restart the database service on it.

2, the Log setting to SQL Server and Windows authentication mode, setting is as follows:

Open SQL Server Management Studio Manager, right-click the server and then view the properties above, the security options choose which authentication service "SQL Server and Windows Authentication mode."

3, changes the password for SQL Server sa, physical setting is as follows:

In SQL Server Management Studio Manager, expand the server on the "Security" -> login name -> right click in the sa account on the property, so that the "General" option page to change the sa login account password. Note that SQL Server2005 in, do not allow the password to set a simple, otherwise it will pass. Then select the page "state" of the registry modification to enable.

4, the database connection string:

There are a variety of database connection strings, such as:

Data Server =. SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sapassSql

Data Server = server name SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sapassSql

Data Server = localhostSQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sapassSql

Data Server =.; Initial Catalog = Northwind; User ID = sa; Password = sapassSql

Data Server = server name; Initial Catalog = Northwind; User ID = sa; Password = sapassSql

The choice of specific versions and SQL Server2005 a relationship, if it is SQL Server 2005 Express version, you must have a "SQLEXPRESS", so if the string is defined as when a variable should be written in Server =. \ SQLEXPRESS.

5,. Net2.0 Framework to register SQL Server database:

Found. Net2.0 Framework installation path, generally installed in "C: WindowsMicrosoft.NETFrameworkv2.0.50727" directory, and then specify the directory in the DOS in the run "ASPNET_REGSQL" command, there will be ASP.NET SQL Server Setup Wizard, Click "Next" and then select the "application service configuration for the SQL Server", then simply click Next, you complete the SQL Server registration interface. NorthWind database, we will see more than a few table below.

6, set the web.config file:

In the application web.config file add the following database connection configuration:





So, we have in Visual Studio 2005 you can easily call the SQL Server data connection.

Now we come to the top of the steps made to test the data connection, we open the Visual Studio 2005, in view of the "Server Explorer" to add the SQL Server 2005 connection, by entering the server name and the corresponding SQL Server Authentication sa user name password, select the corresponding database, then click on "Test Connection", will be successful connection to the database.