Database Reference
In-Depth Information
2. Replace the code in Connection01eDb.cs with that in Listing 12-3. This is
basically the same code as Connection.cs , with the changed code in bold.
Listing 12-3. Connection01eDb.cs
using System;
using System.Data;
using System.Data.OleDb;
namespace Chapter12
{
class ConnectionOleDb
{
static void Main()
{
// Create connection
OleDbConnection conn = new OleDbConnection(@"provider = sqloledb;
data source = .\sql2012; integrated security = sspi;");
try
{
// Open connection
conn.Open();
Console.WriteLine("Connection opened.");
// Display connection properties
Console.WriteLine("Connection Properties:");
Console.WriteLine("\tConnection String: {0}",conn.ConnectionString);
Console.WriteLine("\tDatabase: {0}",conn.Database);
Console.WriteLine("\tDataSource: {0}",conn.DataSource);
Console.WriteLine("\tServerVersion: {0}",conn.ServerVersion);
Console.WriteLine("\tState: {0}",conn.State);
}
catch (OleDbException ex)
{
// Display error
Console.WriteLine("Error: " + ex.Message + ex.StackTrace);
}
finally
{
// Close connection
conn.Close();
Console.WriteLine("Connection closed.");
}
Console.ReadLine();
}
}
}
3. Make ConnectionOleDb the startup project, and run it by pressing Ctrl+F5. If
the connection is successful, you'll see output like that shown in Figure 12-4.
 
Search WWH ::




Custom Search