Cannot open SQL Server database - c#

I downloaded SQL Server 2016 Express and Visual Studio 2015.
I get this message when I try to open SQL Server is my Visual Studio program.
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: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
My code is
// _ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker; User Id = Track;Password =Track;Trusted_Connection=True;";
_ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
SqlConnection myConnection = new SqlConnection(_ConnectionString);
try
{
myConnection.Open();
return "";
}
catch (Exception e)
{
return e.Message + " " + _ConnectionString;
}
I tried my different connection strings. This is one example.
ARKY is the name of my computer and SQLEXPRESS is my named instance.
See the screen shot of my SQL Server database. I will appreciate any suggestions.

Try this:
_ConnectionString = #"Data Source=ARKY\SQLExpress;InitialCatalog=Tracker;Trusted_Connection=True;Connection Timeout=10";
You need a backslash rather than a front slash in your server name, and several parameters are misnamed.
In general, you can open "Server Explorer" in VS, add a data connection to your server there, then click on the data connection and look for the connection string in the properties window. You can cut and paste from there.

Try to use this one
_ConnectionString = "Data Source=ARKY/SQLExpress;Initial Catalog=Tracker;Trusted_Connection=True;Connect Timeout=10";

_ConnectionString = "Server = ARKY\\SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
Check your connection string as well
From FIX : ERROR : Could not open a connection to SQL Server:
Check if your SQL server services is up and running properly:
Go to All Programs > Microsoft SQL Server 2016 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.
In addition, ensure that your remote server is in the same network. Run sqlcmd -L to ascertain if your server is included in your network list.
Enable TCP/IP in SQL Server Configuration
When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2016 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable
You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.

Related

Could not connect to SQL Server but Excel and ODBC config can

I have a C# program to connect to SQL Server. It works fine at the test computer with SQL Server 2012 but does not work on the production environment with 2008. At the production environment, it reports exception as such,
Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or in
stance-specific error occurred while establishing a connection to SQL Server. Th
e server was not found or was not accessible. Verify that the instance name is c
orrect and that SQL Server is configured to allow remote connections. (provider:
Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) --
-> System.ComponentModel.Win32Exception: Access is denied
here is my connection code,
connStrSql = "Server=" + sqlserver + "; Database=" + sqldb + "; Trusted_Connection=True";
SqlConnection sqlConn = new SqlConnection(connStrSql)
sqlConn.Open();
The target platform is x86 and target framework is 4.5. The funny thing is that Excel and ODBC config can connect to the database without complain. Does C# program use different way connecting to SQL Server? How can I fix the problem?
There are many possible reasons why you can not connect to an SQL Server database. This is a great trouble shooting guide to help you solve the above error.
Briefly:
Is the SQL Server services running?
Has the SQL Server TCP/IP settings been configured?
Does the firewall settings allow SQL Server through?
Has SQL Server itself been configured allow remote connections?
Other things to consider
Is the value of sqlserver correct?
Has the client protocol you are using to connection to SQL Server, such as Named Pipes, been enabled?
One of the most common reasons for a remote connection being refused in newer versions of SQL Server is the SQL Browser is not turned on. It is off by default after an install. Another common reason is you don't have the particular protocol (in this case net pipes) turned on, but I would check the SQL Browser first, as it is probably turned off.
Might be firewall/security restriction in production - Try running against a local SQL2008 db because I just ran the code against SQL2008 and it does work with a correct sqlserver and sqldb parameter and change the line below ( missing ; )
SqlConnection sqlConn = new SqlConnection(connStrSql);
Try this for SQL Server connection:
Attach a database file on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;
Try this Excel Connection string
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
If any other problem regarding connection string, refer this site
MS-Excel: http://www.connectionstrings.com/excel/
MS SQL Server: http://www.connectionstrings.com/sql-server/
Thanks,
NB

Error - "The server was not found or was not accessible" when trying to connect to sql server database

I'm working on a local project and trying to connect to a local database.
I have this class in it the connection string:
public class SQLConnection
{
public static string connectionString =
#"Data Source=TZVIKA-PC\SQLEXPRESS;Initial Catalog=WorldCup;Integrated Security=True";
public static WorldCupDBDataContext wcDataContext =
new WorldCupDBDataContext(connectionString);
public static WorldCupDBDataContext GetDataContextInstance()
{
return wcDataContext;
}
}
when I try to execute this line in a windows form:
teamBindingSource.DataSource = database.Teams.OrderBy(team => team.TeamId);
I get the following sql exception:
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)
My friend ran the code and it works on his pc(when changing the connection string of course to a relevant one)
Why is it not working for me?
A good (and easy) way to test if your SQL connection is working is to use a .UDL file. Simply create an empty text (.txt) file on your desktop and rename the extension to .udl. Double click and you'll be presented with a SQL connection window, fill in the details, test connection and then (when it's working) save. Close the window and rename the file extension to .txt. Open and inside you'll find your working connection string.
Enable remote connections for SQL Server Express 2012
Most common problems are:
1) Your sql server express is not running. Check it in "sql server configuration manager"
2) server runs but don't allow remote connection. Use sql management studio to configure it.

ADO.NET: Open SQL Connection

i want to do some sql code run in my webservice in c#
the code is just
[WebMethod]
public void GetCustomers()
{
SqlConnection MyConn = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/WebService2/WebService2/App_Data/Database1.mdf;Integrated Security=True;User Instance=True");
MyConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = MyConn;
cmd.CommandText = "delete from t1 where name='1'"; //just see ["+month
// [mon+"] it's imp
cmd.ExecuteNonQuery();
}
now i get error like
System.Data.SqlClient.SqlException: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Correct Format:Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Try:
"Data Source=./SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WebService2\WebService2\App_Data\Database1.mdf; Database=YourDatabasName;Integrated Security=True;User Instance=True"
For more details see:
http://connectionstrings.com/
You might want to check the configuration of your SQL Server as well.
The error remarks the issue could be the named pipes not being active, this is the default setting.
You can always test this fix by going;
Start -> SQL Server xxxx -> Configuration Tools -> SQL Server
Configuration Manager
Where xxxx is the version of SQL Server you are using.
In the tree look at the
-> SQL Native Client xx Configuration -> Client Protocols
Named Pipes is listed.
Set it to being Enabled
Then under
SQL Server Network Configuration -> Protocols for xxxxxx
xxxxxx Being the name of your SQL Server database instance name.
Check that Named Pipes are Enabled there also.
You will need to restart your SQL Server database in order for it to accept Named Pipe calls.
Might be worth a try.

Sql Connection Problem

I have a sqlconnection problem. i have perfectly connection to sql server when i m using sqldatasource.
but when i m try use SqlConnection object throw an exception.
string qstring = "Data Source=****;Initial Catalog=**;User ID=**;Password=**";
SqlConnection con = new SqlConnection(qstring);
con.Open(); (exception thrown here)
exception is : 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Sql server is in different machine and i can connect using SqlDataSource object for example i can bind a grid by that way. But i must connect with SqlConnection object
Connection string is true because i have it from SqlDataSource...
Thx for your helps..
You might also want to make sure that the SQL Server is set up to accept remote connections.
(for sql2005 Configuration tools -> Surface AreaConfiguration-> Services and Connections -> database, Local and remote connections )
Is your SQL server is in the same machine ? if yes , Check the SQL server service is running , If its in a different machine,check your development machine has connectivity to it .You can ping to that machine to verify this. You can also check the surface area connection wizard to check whether the SQL server support remote connections , as snomag said
If the code that you have shown is what you have then I'm not surprised that you couldn't connect.
You'll need to have the correct Data Source, UserID and Password in qstring. As it stands asterisk characters won't mean anything.
A new point
A further point, which is often forgotten, SQLConnection will only work with Microsoft SQL Server. If your SQL is provided by a different manufacturer the use OleDBConnection instead.

Problem in the connection string of Microsoft SQL Server 2005 !

I have MSSQLServer 2005 installed on my machine. I am creating a connection string like this:
String sqlConnectionString=
user id=admin;
password=admin;
server=MachineName\MSSQLSERVER;
Trusted_Connection=no;
database=MYDataBase;
connection timeout=30
When I do:
myConnection = new SqlConnection(sqlConnectionString);
myConnection.Open();
Open() command throws an exception :
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: 25
Also the same string works fine on SQLEXPRESS/
****I have configured Sql Express to accept remote connections by choosing "Local and remote connection".Also I started SQL Browswer service.****
Is there anything wrong in connection string?
You must have made a typo somewhere. Try this:
Load up Visual Studio.
Go to the "Server Explorer" tool window.
Create a new data connection.
Fill in your required values, click the "Test Connection" button to make sure they work, then click OK.
Click on your new connection in the "Server Explorer", and you can copy and paste the connection string from the Properties tool window.
Have a look at http://www.connectionstrings.com/sql-server-2005
And verify that your names are correct.
Make sure that you have ‘SQL server and Windows Authentication mode’ set under ‘Server authentication’ in ‘Security’ properties. Your connection string implies that you are using SQL authentication. If you have ‘Windows Authentication mode’ set you may receive this error.
Try setting the server name to just the name of your machine, as in ‘server=MachineName;’.

Categories