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( ) ) ;
}
}
}
|