Database Reference
In-Depth Information
Console.WriteLine( "\tState: {0}", conn.State);
Since you display this property after the Open() call, it shows that the connection is open.
State: Open
You've been displaying your own message that the connection is open, but this property contains
the current state. If the connection is closed, the State property would be Closed .
You then display the workstation ID, which is a string identifying the client computer. The
Workstationld property is specific to SQL Server and can be handy for debugging.
Console.WriteLine("\tWorkstationId: {0}",conn.WorkstationId);
It defaults to the computer name. My computer is named VIDYAVRAT, but yours, of course, will be
different.
Workstationld: <YourComputerName>
What makes this useful for debugging is that the SQL Server tools on the server can display which
workstation ID issued a particular command. If you don't know which machine is causing a problem,
you can modify your programs to display the Workstationld property and compare them to the
workstation IDs displayed on the server.
You can also set this property with the workstation ID connection string parameter as follows, so if
you want all the workstations in, say, Building B to show that information on the server, you can indicate
that in the program:
// Connection string
string connString = @" server = \sql2012; workstation id = Building B; integrated security =
true; ";
That completes the discussion of the fundamentals of connecting to SQL Server with SqlClient .
Now let's look at connecting with another data provider.
Connecting to SQL Server with OleDbConnection
As you saw in Chapter 10, you can use the OLE DB data provider to work with any OLE DB-compatible
data store. Microsoft provides OLE DB data providers for Microsoft SQL Server, Microsoft Access (Jet),
Oracle, and a variety of other database and data file formats.
If a native data provider is available for a particular database or file format (such as the SqlClient
data provider for SQL Server), it's generally better to use it rather than the generic OLE DB data provider.
This is because OLE DB introduces an extra layer of indirection between the C# program and the data
source. One common database format for which no native data provider exists is the Microsoft Access
database ( .mdb file) format, also known as the Jet database engine format, so in this case you need to use
the OLE DB (or the ODBC) data provider.
We don't assume you have an Access database to connect to, so you'll use OLE DB with SQL Server.
Try It: Connecting to SQL Server with the OLE DB Data Provider
To connect to SSE with the OLE DB data provider, follow these steps:
1. Add a C# Console Application project named ConnectionOleDb, and rename
Program.cs to Connection01eDb.cs .
 
Search WWH ::




Custom Search