Using data from SQL in C#

K

kanepart2

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.OnError(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(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.OpenLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)\r\n at
System.Data.SqlClient.SqlConnection.Open()\r\n at
SqlSample.Program.Main(String[] args) in
 
A

AlexS

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


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.OnError(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(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.OpenLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenConnection(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.
 
J

Jeremy Shovan

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

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.OnError(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(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.OpenLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenConnection(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.
 
A

Aneesh Pulukkul[MCSD.Net]

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




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.OnError(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(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.OpenLoginEnlist(SqlConnectio­n
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdenti­ty
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOpt­ions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConne­ction
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenConnection(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?
 
E

Eric Renken

It looks to me that you are just missing the server name. I would try
adding "Server=****server name***"

Eric Renken

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.OnError(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(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.OpenLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOpt
ions, String newPassword, Boolean redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo,
String newPassword, SqlConnection owningObject, Boolean
redirectedUserInstance)\r\n at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool,
DbConnection owningConnection)\r\n at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
owningConnection, DbConnectionPool pool, DbConnectionOptions options)\r
\n at
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)\r\n at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)\r\n at S
ystem.Data.ProviderBase.DbConnectionClosed.OpenConnection(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top