Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2006, 12:24 PM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Server open problem

I purchased the book “Professional C# 2nd Edition” by Robinson, Allen, etc. (2002) and am not able to run any of the examples that use SQL Server. For example, the executable ‘DataAccess\01_ExecutingCommands\ ExecutingCommands.exe’ generates an exeception when function SqlConnection.open()is run. The abbreviated error message is:

System.Data.SqlClient.SqlException: An error has occurred while establishing a c onnection to the server. When connecting to SQL Server 2005, this failure may b e 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) at System.Data.SqlClient.SqlConnection.Open() at ExecutingCommands.ExecuteSql(String source) in \C#, 2nd edition\DataAccess\01_ExecutingCommands\ExecutingC ommands.cs:line 48

Can the examples provided in the test book be run as is or is it necessary to somehow initialize the database files (e.g. C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Samples\Setup\ InstNwnd.sql)first?

MS VS NET 2005 and SQL Server (32 bit version) are installed on my computer which is running Win XP Pro.


 
Old August 24th, 2006, 03:35 AM
2V. 2V. is offline
Authorized User
 
Join Date: Aug 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Ibell,

First of all: by default your SQL server and C# Application must be on 1 computer.

try it:
//CODE
string connectionString = "user id=ANY_user;password=ANY_PASS;server=YOUR_COMP_NAM E\\SQLEXPRESS;Trusted_Connection=yes; database=MySqlTestDb; connection timeout=30";
SqlConnection myConnection = new SqlConnection(connectionString );
myConnection.Open();
//END_CODE
connection string:
The User ID is used when you are using SQL Authentication, but here this is ignored, because of using Trusted_Connection.
MySqlTestDb - such database must exist in folder
"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\MySqlTestDb.mdf"
you can create it with "Server Explorer" utility in Visual Studio or use some standart db

 
Old August 24th, 2006, 05:19 PM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the suggestion but it produces a similar error message.
I have included the full error message here. In addition, I have added the sample code that generates this message. Please note the database file you recommended is not found in the specified directory. However, the NorthWind database was and this is what I attempted to read. This database file was copied to the directory containing the C# source code.

Thank you for any comments and/or suggestions.


Error message:

System.Data.SqlClient.SqlException: Cannot open database "northwnd" requested by
 the login. The login failed.
Login failed for user 'DA3\Ian Bell'.
   at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception
, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObj
ect stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.Com pleteLogin(Boolean enlis
tOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.Ope nLoginEnlist(SqlConnecti
on owningObject, SqlConnectionString connectionOptions, String newPassword, Bool
ean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ct or(DbConnectionPoolIdent
ity identity, SqlConnectionString connectionOptions, Object providerInfo, String
 newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateC onnection(DbConnectionOp
tions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection
 owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.Creat ePooledConnection(DbConn
ection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection owning
Object)
   at System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection o
wningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection ownin
gObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection ow
ningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(DbConnection ou
terConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at ExecutingCommands.ExecuteSql(String source) in C:\Documents and Settings\I
an Bell\My Documents\temp\testCSConsoleApplication\testCSCons oleApplication\Exec
utingCommands.cs:line 37



Sample Code:

using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Data.OleDb;

/// <summary>
/// Corresponds to section titled 'Executing Commands' in Chapter 11
/// </summary>
public class ExecutingCommands
{
    /// <summary>
    /// SimpleDataAccess - show SQL & Stored Procs
    /// </summary>
    public static void Main ( )
    {
        string source = "user id=ANY_user;password=ANY_PASS;server=da3\\SQLEXPRE SS;Trusted_Connection=yes; database=northwnd; connection timeout=30";
                // The following is the database connection string
        //string source = "data source=(local);initial catalog=Northwind;integrated security=SSPI;";

        // First section of code - using a SQL statement to select records
        ExecuteSql ( source ) ;

    }

    public static void ExecuteSql ( string source )
    {
        // And this is the SQL statement that will be issued
        string select = "SELECT ContactName,CompanyName FROM Customers";

        try
        {
            // Connect to the database...
            using ( SqlConnection conn=new SqlConnection(source) )
            {
                // Open the database connection
                conn.Open ( ) ;

                // Create the SQL command...
                SqlCommand cmd = new SqlCommand ( select , conn ) ;

                // Construct the data reader
                using ( SqlDataReader reader = cmd.ExecuteReader ( ) )
                {
                    // Output headings...
                    Console.WriteLine ( "*** SqlProvider ***" ) ;
                    Console.WriteLine ( "Output from direct SQL statement..." ) ;
                    Console.WriteLine ( ) ;
                    Console.WriteLine ( "CONTACT COMPANY" ) ;
                    Console.WriteLine ( "---------------------------------------------------------------------" ) ;

                    // And iterate through the data
                    while ( reader.Read ( ) )
                    {
                        Console.WriteLine ( "{0,-30} {1}" , reader[0] , reader[1] ) ;
                    }

                    reader.Close ( ) ;
                }

                conn.Close ( ) ;
            }
        }
        catch ( Exception e )
        {
            Console.WriteLine ( e.ToString( ) ) ;
        }
    }
}





 
Old August 25th, 2006, 03:50 AM
2V. 2V. is offline
Authorized User
 
Join Date: Aug 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, Are you shure that your database "northwnd" exist ???
Ok, try next: In MVS 2005 Menu choose View - Server Explorer. than - data Connection - <right click> Add connection... SQL Server.. Continue.. "Server Name" (compname\servname) - Choose available server name "Select or enter db" - Choose your db. Press button Test Connection ...
and one more thing: <SQL Server(SQLEXPRESS)> and <SQL Server Browser> - this services must be runned.

 
Old August 25th, 2006, 10:43 AM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Ukraine,

This still does not work and both the 'Northwind.mdb' and 'Northwind.mdf' files exists. I've just ordered a book that deals exclusively with programming data bases. I will wait until it arrives and hopefully it will contain sufficiently detailed information so that I can work through the examples.

Thanks again for your time and effort. It was good of you to trying helping me out.

Ian






Similar Threads
Thread Thread Starter Forum Replies Last Post
20599 - Cannot open SQL server Anup Gavate VB Components 2 March 5th, 2009 04:49 AM
20599 - Cannot open SQL Server itsviji4u SQL Server 2005 0 November 3rd, 2007 06:16 AM
Cannot open SQL Server kxradhi Pro VB Databases 0 September 18th, 2006 09:13 PM
Could not open a connection to the Sql Server sureshkumar1981 ADO.NET 1 May 19th, 2006 03:59 AM
SQL Server cannot open Jane SQL Server 2000 5 December 30th, 2004 01:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.