469,299 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Using data from SQL in C#

I am making a simple program to use display SQL data using C#

SqlConnection myConnection = new
SqlConnection(@"Trusted_Connection=yes;database=** ***database
name*****;connection timeout=30");

try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
above is the code snippet where I run into problems..
myConnection.Open times out and the exception gets thrown. I have no
clue why!

the following is the content of the exception

-----------------------------------------------------------------------------------------------------------------------------
"System.Data.SqlClient.SqlException: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj)\r\n at System.Data.SqlClient.TdsParser.Connect(Boolean&
useFailoverPartner, Boolean& failoverDemandDone, String host, String
failoverPartner, String protocol, SqlInternalConnectionTds
connHandler, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenCon nection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
-------------------------------------------------------------------------------------------------------------------------------------------

My deduction is that the connection to the server is not successful

any ideas would be appreciated.

Jun 7 '07 #1
4 3610
You are right, you can't connect.

If you use SQL2005, check Surface Area manager how your connections are set
up.

For older versions, Client Configuration tool should help

HTH
Alex
<ka*******@hotmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>I am making a simple program to use display SQL data using C#

SqlConnection myConnection = new
SqlConnection(@"Trusted_Connection=yes;database=** ***database
name*****;connection timeout=30");

try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
above is the code snippet where I run into problems..
myConnection.Open times out and the exception gets thrown. I have no
clue why!

the following is the content of the exception

-----------------------------------------------------------------------------------------------------------------------------
"System.Data.SqlClient.SqlException: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj)\r\n at System.Data.SqlClient.TdsParser.Connect(Boolean&
useFailoverPartner, Boolean& failoverDemandDone, String host, String
failoverPartner, String protocol, SqlInternalConnectionTds
connHandler, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenCon nection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
-------------------------------------------------------------------------------------------------------------------------------------------

My deduction is that the connection to the server is not successful

any ideas would be appreciated.


Jun 7 '07 #2
If you are using SQL Server 2005. Open up the the Surface Area Configuration
tool then do the following
1. Click the "Surface Are Configuration for Services and Connections" link
button.
2. Select the DB Server instance you are trying to connect to then go to the
remote connections settings.
[db instance]/database engine/remote connections
3. Select the "local and remote connections" radio button
4. Chose whether to connect using TCP/IP or named pipes.
5. Your done..

If that doesn't work check that your connection string is right.. you can
get sample connection strings for variouse DBMS's at
http://connectionstrings.com/
Jeremy Shovan
http://www.jeremyshovan.com

<ka*******@hotmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>I am making a simple program to use display SQL data using C#

SqlConnection myConnection = new
SqlConnection(@"Trusted_Connection=yes;database=** ***database
name*****;connection timeout=30");

try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
above is the code snippet where I run into problems..
myConnection.Open times out and the exception gets thrown. I have no
clue why!

the following is the content of the exception

-----------------------------------------------------------------------------------------------------------------------------
"System.Data.SqlClient.SqlException: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj)\r\n at System.Data.SqlClient.TdsParser.Connect(Boolean&
useFailoverPartner, Boolean& failoverDemandDone, String host, String
failoverPartner, String protocol, SqlInternalConnectionTds
connHandler, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenCon nection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
-------------------------------------------------------------------------------------------------------------------------------------------

My deduction is that the connection to the server is not successful

any ideas would be appreciated.
Jun 7 '07 #3
On Jun 7, 9:13 pm, "AlexS" <salexru200...@SPAMrogers.comPLEASEwrote:
You are right, you can't connect.

If you use SQL2005, check Surface Area manager how your connections are set
up.

For older versions, Client Configuration tool should help

HTH
Alex

<kanepa...@hotmail.comwrote in message

news:11**********************@o11g2000prd.googlegr oups.com...
I am making a simple program to use display SQL data using C#
SqlConnection myConnection = new
SqlConnection(@"Trusted_Connection=yes;database=** ***database
name*****;connection timeout=30");
try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
above is the code snippet where I run into problems..
myConnection.Open times out and the exception gets thrown. I have no
clue why!
the following is the content of the exception
---------------------------------------------------------------------------*--------------------------------------------------
"System.Data.SqlClient.SqlException: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObje*ct
stateObj)\r\n at System.Data.SqlClient.TdsParser.Connect(Boolean&
useFailoverPartner, Boolean& failoverDemandDone, String host, String
failoverPartner, String protocol, SqlInternalConnectionTds
connHandler, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnectio*n
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdenti*ty
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOpt*ions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConne*ction
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenCon nection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
---------------------------------------------------------------------------*----------------------------------------------------------------
My deduction is that the connection to the server is not successful
any ideas would be appreciated.- Hide quoted text -

- Show quoted text -
Is it because of Trusted_Connection property?

Jun 7 '07 #4
It looks to me that you are just missing the server name. I would try
adding "Server=****server name***"

Eric Renken

<ka*******@hotmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>I am making a simple program to use display SQL data using C#

SqlConnection myConnection = new
SqlConnection(@"Trusted_Connection=yes;database=** ***database
name*****;connection timeout=30");

try
{
myConnection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
above is the code snippet where I run into problems..
myConnection.Open times out and the exception gets thrown. I have no
clue why!

the following is the content of the exception

-----------------------------------------------------------------------------------------------------------------------------
"System.Data.SqlClient.SqlException: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject
stateObj)\r\n at System.Data.SqlClient.TdsParser.Connect(Boolean&
useFailoverPartner, Boolean& failoverDemandDone, String host, String
failoverPartner, String protocol, SqlInternalConnectionTds
connHandler, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenCon nection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
-------------------------------------------------------------------------------------------------------------------------------------------

My deduction is that the connection to the server is not successful

any ideas would be appreciated.

Jun 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by pmud | last post: by
14 posts views Thread by Steve Teeples | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.