简体   繁体   中英

sql 2008 server connection

hey can someone tell me what is the problem here?

SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = "server=zekyakad-7727\\sqlexpress.master.dbo;database=master;";
sqlConn.Open(); //error line

Error msg: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Could be any number of things:

  • The connection string is not correct
  • There is no network access between the computer running this and the SQL Server
  • There is a firewall blocking the specific required ports
  • SQL Server is not running
  • SQL Server is not configured to allow remote connections
  • You have not supplied any credentials or setup the connection to Windows Auth
  • The user does not have login permissions to SQL Server

So:

  • Ensure you are using the correct connection strings
  • Make sure SQL Server is running
  • Check the network and that you can connect to SQL Server
  • Configure SQL Server for remote connections
  • Make sure you are providing valid credentials

To be more concrete:

You set your Server in the connection string to be:

zekyakad-7727\\sqlexpress.master.dbo

This does not look like the name of a server . This should probably be zekyakad-7727 .

Take a look at connectionstrings.com to see what valid connection strings should look like.

Check the following things.

  1. Make sure your database engine is configured to accept remote connections • Start > All Programs > SQL Server 2005 > Configuration Tools > SQL Server Surface Area Configuration • Click on Surface Area Configuration for Services and Connections • Select the instance that is having a problem > Database Engine > Remote Connections • Enable local and remote connections • Restart instance

  2. Check the SQL Server service account • If you are not using a domain account as a service account (for example if you are using NETWORK SERVICE), you may want to switch this first before proceeding

  3. If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings in your ASweb P.NET application • Usually the format needed to specify the database server is machinename\\instancename • Check your connection string as well

  4. You may need to create an exception on the firewall for the SQL Server instance and port you are using

• Start > Run > Firewall.cpl • Click on exceptions tab • Add the sqlservr.exe (typically located in C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL.x\\MSSQL\\Binn), and port (default is 1433) • Check your connection string as well

<connectionStrings>
<add name=”SampleConnectionString” connectionString=”Data Source=machinename\in
stancename;Initial Catalog=AdventureWorks;Integrated Security=SSPI;Min  Pool Size=5;Max     Pool Size=60;Connect Timeout=30″ providerName=”System.Data.SqlClient”/>

</connectionStrings>

5.If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings

6.Check SQLBrowser; check that it is running. You may also need to create an exception in your firewall for SQLBrowser.

7.Check that you have connectivity to the SQL Server. Note what you are using to connect: machine name, domain name or IP address? Use this when checking connectivity. For example if you are using myserver • Start > Run > cmd •netstat -ano| findstr 1433 •telnet myserver 1433 •ping -a myserver

Check what ports are IP addresses are being returned.

Alternative: If you still can't get any connection, you may want to create a SQL account on the server, a corresponding SQL user on the database in question, and just use this username/password combo in your web application.

Rather than entering the connectionstring this way, I would suggest you to use the SqlConnectionStringBuilder in the following way. This is less error prone.

SqlConnection dataConnection = new SqlConnection();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = ".\\SQLExpress"; // Your Datasource
builder.InitialCatalog = "Northwind"; // Database Name
builder.IntegratedSecurity = true;
dataConnection.ConnectionString = builder.ConnectionString;
dataConnection.Open();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM