Sql Connection Problem - c#

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.

Related

How to access database in SQL Server 2008 from another computer

I need to access database in another computer using c#...I am using connection string with IP address for accessing database and also changed firewall setting but it shows error as follows
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: TCP
Provider, error: 0 - A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.)
I m using this connection string
SqlConnection con = new SqlConnection("data source=192.168.1.12,1063;initial catalog=trinity;integrated security=false;network library=DBMSSOCN");
This is actually quite a large topic involving: Networking, Authentication, Authorization and Permissions. Quick checklist:
Open port 1433 on local firewall where SQL Server is running.
Open SQL Server Configuration Manager on the local machine where SQL Server is running and enable TCP connections.
Add a Windows Login or SQL Account user and grant permissions to the database. DON'T USE THE SA ACCOUNT TO CONNECT REMOTELY EVER!.
In your connection string you have Integrated security=false;, so you need to provide User ID=mysqlserveruserid;Password=mysqlserverpassword; parameters in your connection string instead. If you change Integrated security=true; your Windows credentials will be used.
Also why the IP Address? Do you not have network name resolution on your network? e.g. DNS or WINS.
Is SQL Server running as the default instance or does it have a named instance? If it's a named instance (would have been a choice taken when installing SQL Server) You will need Data source=computername\sqlserverinstancename;
More detailed information on all these aspects can be found on MSDN
https://msdn.microsoft.com/en-us/library/ms345332.aspx
Try given solutions, it may help you
http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx

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

Connecting to a database

I'm trying to connect to a database but nothing I try works.
SqlConnection conn = new SqlConnection(#"Data Source=C:\Users\Gerard Foley\Desktop\Northwind.sdf");
conn.Open();
No matter what I try I just get the error:
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)
I stole the connection string from Database Explorer -> Properties -> Connection String. What am I missing? I can get the tables to show up in a DataGridView fine (by dragging from Data Sources), but I want to use my own UI and queries. I just can't seem to figure this ADO thing out.
Using c# express 2010 and sql server express 2008.
for the proper connection string to use to connect to SQL Server have a look at:
http://connectionstrings.com
the connection string you are using now is strange, it should contain server name and database name, see link above for examples...
GOT IT. I should have been using Sql*Ce*Connection. The connection string was fine.
You need to specify 'AttachDbFileName' in the connection string. See the examples for sql server express here: http://www.connectionstrings.com/sql-server-2008.

Sql Server 2005 localhost System DSN not working

I've got an C# .Net 3.5 executable that runs on a local machine and makes database calls to a server running SQL Server 2005. I've added a System DSN using the Data Sources manager on the local machine called "localserver" (with the driver set to SQL Server). When I create the DSN, I can test the connection to the server successfully.
I've added localserver to my connection string in my executable, and I get an error "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)"
The server I am connecting to does allow for remote connections. When I replace localserver with the server name, it connects fine, but this program will be at multiple locations with multiple server names, so I need to make the data source dynamic.
Any ideas as to why this is happening?
Thank you,
Aaron
EDIT:
I've also tried using "localserver,1433" as my data source, but I get this error:
"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: TCP Provider, error: 0 - No such host is known.)"
EDIT:
Thank you for your responses. To solve my problem, I made a small method to gather the servername using an odbc connection.
I can then send the servername to the SqlConnection. Thanks again for your input.
SqlClient (ie. SqlConnection) has absolutely nothing to do with ODBC. As such using an ODBC Data source Name in the SqlClient connection string will get you nowhere fast.
You can configure the server name in app.config and build the connection string using SqlConnectionStringBuilder. At deployment, you change the exe's or the user's .config file appropriately.
As Remus said, DSN has nothing to do with SqlConnection. Instead use this connection string:
http://www.connectionstrings.com/sql-server-2005#1
Also read this:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
An Excerpt from the above post:
To connect to a local computer,
specify "(local)" for the server. If a
server name is not specified, a
connection will be attempted to the
default instance on the local
computer.
I would repeat that SqlConnection has nothing to do with DSN,

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