- •Table of Contents
- •C# and the .NET Platform, Second Edition
- •Introduction
- •Part One: Introducing C# and the .NET Platform
- •Part Two: The C# Programming Language
- •Part Three: Programming with .NET Assemblies
- •Part Four: Leveraging the .NET Libraries
- •Part Five: Web Applications and XML Web Services
- •Obtaining This Book's Source Code
- •The .NET Solution
- •What C# Brings to the Table
- •The Role of the Assembly Manifest
- •Summary
- •Chapter 2: Building C# Applications
- •Summary
- •Chapter 3: C# Language Fundamentals
- •Defining Program Constants
- •Defining Custom Class Methods
- •C# Enumerations
- •Summary
- •The Second Pillar: C#'s Inheritance Support
- •Summary
- •Catching Exceptions
- •Finalizing a Type
- •Garbage Collection Optimizations
- •Summary
- •Chapter 6: Interfaces and Collections
- •Building Comparable Objects (IComparable)
- •Summary
- •Summary
- •Internal Representation of Type Indexers
- •Summary
- •An Overview of .NET Assemblies
- •Understanding Delayed Signing
- •Using a Shared Assembly
- •GAC Internals
- •Summary
- •Spawning Secondary Threads
- •A More Elaborate Threading Example
- •Summary
- •Summary
- •Object Persistence in the .NET Framework
- •The .NET Remoting Namespaces
- •Understanding the .NET Remoting Framework
- •All Together Now!
- •Terms of the .NET Remoting Trade
- •Testing the Remoting Application
- •Revisiting the Activation Mode of WKO Types
- •Deploying the Server to a Remote Machine
- •Summary
- •Control Events
- •The Form Class
- •Summary
- •Regarding the Disposal of System.Drawing Types
- •Understanding the Graphics Class
- •Summary
- •The TextBox Control
- •Working with Panel Controls
- •Configuring a Control's Anchoring Behavior
- •Summary
- •Chapter 16: The System.IO Namespace
- •The Static Members of the Directory Class
- •The Abstract Stream Class
- •Summary
- •The Role of ADO.NET Data Providers
- •The Types of System.Data
- •Selecting a Data Provider
- •The Types of the System.Data.OleDb Namespace
- •Working with the OleDbDataReader
- •Summary
- •Submitting the Form Data (GET and POST)
- •Some Benefits of ASP.NET
- •Creating an ASP.NET Web Application by Hand
- •The Composition of an ASP.NET Page
- •The Derivation of an ASP.NET Page
Selecting a Data Provider |
|
C# nd the .NET Platform, Second Edition |
|
by Andrew Troelsen |
ISBN:1590590554 |
As mentioned earlier in this chapter, .NET 1.1 ships with five data providers. The first of these is the OleDb
Apress © 2003 (1200 pages)
data provider, which is composed of the types defined in the System.Data.OleDb namespace. The OleDb
This comprehensive text starts with a brief overview of the
provider allows youC#tolanguageaccessanddatathenlocatedquicklyin anymovesdatatostorekey technicalthat supportsand the classic OLE DB protocol. Thus, like with classicrchitecturalADO, youissuesmayforuse.NETthedevelopersADO.NET. data provider to access SQL Server, Oracle, or
MS Access databases. Because the types in the System.Data.OleDb namespace must communicate with unmanaged code (e.g., the OLE DB providers), you need to be aware that a number of .NET to COM
Table of Contents
translations occur behind the scenes, which can affect performance. By and large, this namespace is
C# and the .NET Plaretform,attemptingSeco d Edition
useful when you to communicate with a data source that does not have a specific data
Intrproviderductionassembly.
Part One - Introducing C# and the .NET Platform
The SQL data provider offers direct access to MS SQL Server data stores, and only SQL Server data
Chapter 1 - The Philosophy of .NET
stores (version 7.0 and greater). The System.Data.SqlClient namespace contains the types used by the
Chapter 2 - Building C# Applications
SQL provider and provides the same functionality as the OleDb provider. In fact, for the most part, both
Part Two - The C# Programming Language
namespaces have similarly named items. The key difference is that the SQL provider does not use the
Chapter 3 - C# Language Fundamentals
OLE DB or classic ADO protocols and thus offers numerous performance benefits. |
|
Chapter 4 - Object-Oriented Programming with C# |
|
Chapter 5 |
- Exceptions and Object Lifetime |
If you are interested in making use of the System.Data.Oracle, System.Data.Odbc, or |
|
Chapter 6 |
- Interfaces and Collections |
System.Data.SqlServerCe namespaces, I will assume you will check out the details as you see fit. |
|
Chapter 7 |
- Callback Interfac s, Delegates, and Events |
However, as you would hope, once you are comfortable with one data provider, you can easily manipulate |
|
Chapter 8 |
- Advanced C# Type Construction Techniques |
other providers. Recall that while the exact names of the types will differ between namespaces (for
Partexample,Three -OleDbConnectionProgramming withvs.NET. SqlConnectionAssemblies vs. OdbcConnection), semantically related types can be
Chapttreatedr 9in a- polymorphicUnderstandingmanner.NET Assembliesgiven the IDbCommand, IDbConnection, IDbDataAdapter, and
ChapterIDataReader10 - Procinterfacessses, AppDomains,et al. Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
To begin, we'll examine how to connect to a data source using the OleDb data provider, therefore don't
Part Four - Leveraging the .NET Libraries
forget to specify the proper using directives (recall this data provider is contained within the
Chapter 12 - Object Serialization and the .NET Remoting Layer
System.Data.dll assembly):
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
// Using the OleDb data provider.
Chapter 15 - Programming with Windows Forms Controls
using System.Data;
Chapter 16 - The System.IO Namespace
using System.Data.OleDb;
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Once we have checked out how to interact with a data store using the OleDb data provider, we will see
Chapter 19 - ASP.NET Web Applications
how to make use of the types within the System.Data.SqlClient namespace.
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The Types of the System.Data.OleDb Namespace |
|
C# and the .NET Platform, Second Edition |
|
by Andrew Troelsen |
ISBN:1590590554 |
Table 17-12 provides a walkthrough of the core types of the System.Data.OleDb namespace. Note that
Apress © 2003 (1200 pages)
each type sports an "OleDb-" prefix.
This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and
|
|
|
architectural issues for .NET deve opers. |
|
|
Table 17-12: Key Types of the System.Data.OleDb Namespace |
|||
|
|
|
|
|
|
|
|
System.Data.OleDb |
Meaning in Life |
TableTypeof Contents |
|
|||
|
|
|
|
|
C# and the .NET Platform, Second |
Edition |
|||
|
|
|
OleDbCommand |
Represents a SQL query command to be made to a data source. |
|
Introduction |
|
||
|
|
|||
|
|
|
|
PartOleDbConnectionOne - Introduci g C# and the .RepresentsNET Platforman open connection to a data source.
|
|
|
|
|
|
|
|
Chapter 1 |
- The Philosophy of .NET |
|
|||
|
|
OleDbDataAdapter |
|
Represents a set of data commands and a database connection |
|
|
|
Chapter 2 |
- Building C# Applications |
|
|||
|
|
|
|
|
used to fill and update the contents of a DataSet. |
|
Part Two - The C# Programming |
|
Language |
|
|||
|
|
|||||
ChapterOleDbDataReader3 - C# Language FundamentalsProvides a way of reading a forward-only stream of data records |
|
|||||
Chapter 4 |
- Object-Oriented Programfrominga datawithsource.C# |
|
||||
|
|
|
|
|
|
|
|
|
Chapter 5 |
- Exceptions and Object |
|
Lifetime |
|
|
|
OleDbErrorCollection |
|
OleDbErrorCollection maintains a collection of warnings or errors |
|
|
|
Chapter 6 |
- Interfaces and Collections |
|
|||
|
|
OleDbError |
|
returned by the data source, each of which is represented by an |
|
|
|
Chapter 7 |
- Callback Interfaces, Delegates, and Events |
|
|||
|
|
|
|
|
OleDbError type. When an error is encountered, an exception of |
|
|
ChapterOleDbException8 - Advanced C# Type Construction Techniques |
|
||||
|
|
|
|
|
type OleDbException is thrown. |
|
|
|
|
|
|
||
Part Three - Programming with .NET Assemblies |
|
|||||
|
|
OleDbParameterCollection |
|
Much like classic ADO, the OleDbParameterCollection collection |
|
|
Chapter 9 |
- Understanding .NET Assemblies |
|
||||
|
|
|
|
|
holds onto the parameters sent to a stored procedure in the |
|
ChapterOleDbParameter10 - Processes, AppDomains, Contexts, and Threads |
|
|||||
|
|
|
|
|
database. Each parameter is of type OleDbParameter. |
|
|
|
Chapter 11 |
- Type Reflection, Late |
|
Binding, and Attribute-Based Programming |
|
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
Working withC# andthetheConnected.NET Platform, LayerSecond Editionof ADO.NET
by Andrew Troelsen |
ISBN:1590590554 |
The first step to take when working with the OleDb data provider is to establish a session with the data source
Apress © 2003 (1200 pages)
using the OleDbConnection type (which, as you recall, implements the IDbConnection interface). Much like the
This comprehensive text starts with a brief overview of the
classic ADO ConnectionC# languageobject,andOleDbConnectionthen quickly m vestypesto keyaretechnicalprovidedandwith a formatted connection string, containing a numberarchitecturalof name/valueissues forpairs.NETseparateddevelopersby.semicolons. This information is used to identify the nam of the machine you wish to connect to, required security settings, the name of the database on that machine, and, most importantly, the name of the OLE DB provider. (See online help for a full description of each
Table of Contents name/value pair.)
C# and the .NET Platform, Second Edition
The connection string may be set using the OleDbConnection.ConnectionString property or including it as a
Introduction
constructor argument. Assume you wish to connect to the Cars database on your local machine using the SQL
Part One - Introducing C# and the .NET Platform
OLE DB provider. The following logic does the trick:
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Part// TwoBuild- TheaC#connectioProgrammingstringLangua .e
ChapterOleDbConnection3 - C# Languagecn Fundamentals= new OleDbConnection();
cn.ConnectionString = "Provider=SQLOLEDB.1;" +
Chapter 4 - Object-Oriented Programm ng with C#
"User ID=sa;Pwd=;Initial Catalog=Cars;" +
Chapter 5 - Exceptions and Object Lifetime
"Data Source=(local);";
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 8 - Advanced C# Type Construction Techniques
As you can infer from the preceding code comments, the Initial Catalog name refers to the database you are
Part Three - Programming with .NET Assemblies |
||||||||||
|
|
attempting to establish a session with (Pubs, Northwind, Cars, and so on). The Data Source name identifies the |
||||||||
Chapter 9 |
- Understanding .NET Assemblies |
|||||||||
|
|
name of the machine that maintains the database (for simplicity, I have assumed no specific password is requir |
||||||||
Chapter 10 |
- Processes, AppDomains, Contexts, and Threads |
|||||||||
|
|
for local system administrators). The final point of interest is the Provider segment, which specifies the name of |
||||||||
Chapter 11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
|||||||||
|
|
the OLE DB provider that will be used to access the data store. Table 17-13 describes some possible values. |
||||||||
Part Four - Leveraging the .NET Libraries |
||||||||||
Chapter 12 |
- Object Serialization and the .NET Remoting Layer |
|||||||||
|
|
Table 17-13: Core OLE DB Providers |
||||||||
|
|
|
Chapter 13 |
- Building a Better Window (Introducing Windows Forms) |
|
|
|
|||
|
|
|
ChapterProvider14 - A BetterSegmentPainting FrameworkMeaning(GDI+)in Life |
|
|
|
||||
|
|
|
ChapterValue15 |
- Programming with |
|
Windows Forms Controls |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Chapter 16 |
- The System.IO Namespace |
|
|
|
|||
|
|
|
Microsoft.JET.OLEDB.4.0 |
|
|
You want to use the Jet OLE DB provider to connect to an Access |
|
|
|
|
|
|
|
Chapter 17 |
- Data Access with ADO.NET |
|
|
|
|||
|
|
|
|
|
|
|
database. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Part Five - Web Applications and |
|
|
XML Web Services |
|
|
|
||||
|
|
|
MSDAORA |
|
|
You want to use the OLE DB provider for Oracle. |
|
|
|
|
|
Chapter 18 |
- ASP.NET Web Pages |
|
|
and Web Controls |
|
|
|
||
|
|
|
||||||||
|
|
|
Chapter 19 |
- ASP.NET Web Applications |
|
|
|
|||
|
|
|
SQLOLEDB |
|
|
You want to use the OLE DB provider for MS SQL Server. Once you have |
|
|
|
|
|
Chapter 20 |
- XML Web Services |
|
|
configured the connection string, the next step is to open a session with |
|
|
|
||
Index |
|
|
|
the data source, do some work, and release your connection to the data |
|
|
|
|||
List of Figures |
|
|
source, as shown in the code snippet following this table. |
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
List of Tables |
|
|
|
|
|
|
// Build a connection string.
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = "Provider=SQLOLEDB.1;" +
"User ID=sa;Pwd=;Initial Catalog=Cars;" +
"Data Source=(local);";
cn.Open();
// Do some interesting work here...
cn.Close();
In addition to the ConnectionString, Open(), and Close() members, the OleDbConnection class provides a number of members that let you configure attritional settings regarding your connection, such as timeout setting and transactional information. Table 17-14 gives a partial rundown.
Table 17-14: Members of the OleDbConnection Type
C# and the .NET Platform, Second Edition
|
|
OleDbConnectionby Andrew TroelsenMeaning in Life |
ISBN:1590590554 |
||||
|
|
Member |
Apress © 2003 (1200 pages) |
|
|
||
|
|
|
|
|
|
|
|
|
|
|
This comprehensive text starts with a brief overview of the |
|
|||
|
|
BeginTransaction() |
|
Used to programmatically commit, abort, or roll back a transaction. |
|
||
|
|
|
C# language and then quickly moves to key technical and |
|
|||
|
|
|
architectural issues for .NET developers. |
|
|
||
|
|
CommitTransaction() |
|
|
|
|
|
|
|
RollbackTransaction() |
|
|
|
|
|
|
|
|
|
|
|
||
Table of Contents |
|
Closes the connection to the data source. |
|
||||
|
|
Close() |
|
|
|
||
|
|
|
|
|
|||
C# and the .NET Platform, Second |
|
Edition |
|
|
|||
|
|
ConnectionString |
|
Gets or sets the string used to open a session with a data store. |
|
||
|
Introduction |
|
|
|
|
||
|
|
|
|
||||
|
|
ConnectionTimeout |
|
This read-only property returns the amount of time to wait while |
|
||
Part One - Introducing C# and the .NET Platf rm |
|
|
|||||
Chapter 1 |
|
|
establishing a connection before terminating and generating an error (the |
|
|||
- The Philosophy of .NET |
|
|
|||||
Chapter 2 |
- Building C# Applicationsdefault value is 15 seconds). If you wish to change this default, specify a |
|
|||||
Part Two - The C# Programming Language"Connect Timeout" segment in the connection string (e.g., Connect |
|
||||||
Chapter 3 |
- C# Language FundamentalsTimeout=30). |
|
|
||||
|
|
|
|
|
|
||
|
|
Chapter 4 |
- Object-Oriented |
Programming with C# |
|
|
|
|
|
Database |
|
Gets the name of the database maintained by the connection object. |
|
||
|
|
Chapter 5 |
- Exceptions and Object Lifetime |
|
|
||
|
|
|
|
||||
|
ChapterDataSource6 - Interfaces and CollectionsGets the location of the database maintained by the connection object. |
|
|||||
|
|
|
|
|
|
||
|
|
Chapter 7 |
- Callback Interfaces, |
|
Delegates, and Events |
|
|
|
|
Open() |
|
|
Opens a database connection with the current property settings. |
|
|
|
|
Chapter 8 |
- Advanced C# Type |
|
Construction Techniques |
|
|
|
|
|
|
|
|||
PartGetOleDbSchemaTable()Three - Programming with |
.NETObtainsAssembliesschema information from the data source. |
|
|||||
|
|
|
|
|
|
||
|
|
Chapter 9 |
- Understanding .NET |
|
Assemblies |
|
|
|
|
Provider |
|
|
Gets the name of the provider maintained by the connection object. |
|
|
|
|
Chapter 10 |
- Processes, AppDomains, Contexts, and Threads |
|
|
||
|
|
|
|
||||
|
ChapterState11 |
- Type Reflection, LateGetsBinding,theandcurrentAttributestate-Basedof theProgrammingconnection, represented by the |
|
||||
Part Four - Leveraging the .NET |
LibrariesConnectionState enumeration. |
|
|||||
|
|
|
|
|
|
|
|
Chapter 12 |
- Object Serialization and the .NET Remoting Layer |
|
|
||||
Chapter 13 |
- Building a Better Window (Introducing Windows Forms) |
As you can see, the properties of the OleDbConnection type are typically read-only in nature, and are only usef
Chapter 14 - A Better Painting Framework (GDI+)
when you wish to obtain the characteristics of a connection at runtime. When you wish to override default
Chapter 15 - Programming with Windows Forms Controls
settings, you must alter the construction string itself. For example, ponder the following code, which changes th
Chapter 16 - The System.IO Namespace
default connection timeout setting from 15 seconds to 30 seconds (via the Connect Timeout segment of the
Chapter 17 - Data Access with ADO.NET connection string):
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
OleDbConnection cn = new OleDbConnection();
Chapter 19 - ASP.NET Web Applications |
|
cn.ConnectionString = "Provider=SQLOLEDB.1;" + |
|
Chapter 20 - XML Web Services |
|
|
"User ID=sa;Pwd=;Initial Catalog=Cars;" + |
Index |
"Data Source=(local);Connect Timeout=30"; |
|
List of Figures
cn.Open();
List of Tables
Console.WriteLine("***** Info about your connection *****");
Console.WriteLine("Database location: {0}", cn.DataSource); Console.WriteLine("Database name: {0}", cn.Database); Console.WriteLine("Provider: {0}", cn.Provider); Console.WriteLine("Timeout: {0}", cn.ConnectionTimeout); Console.WriteLine("Connection state: {0}", cn.State.ToString()); cn.Close();
Console.WriteLine("Connection state: {0}", cn.State.ToString());
Notice that this connection is explicitly opened and closed each time before making a call to the State property. As mentioned in the previous table, this property may take any value of the ConnectionState enumeration:
public enum System.Data.ConnectionState
{
Broken, Closed,
Connecting, Executing,
|
C# and the .NET Platform, Second Edition |
|
|
Fetching, Open |
ISBN:1590590554 |
} |
by Andrew Troelsen |
|
Apress © 2003 (1200 pages) |
|
|
|
|
This comprehensive text starts with a brief overview of the
While it may be enticingC# languageto ponderand thenthe notionquicklyofmovesaskingtoakeyconnectiontechnical typeand if it is currently in the process of
architectural issues for .NET developers.
querying data (e.g., ConnectionState.Executing), do understand that with the current release of the .NET Framework, the only valid ConnectionState values are ConnectionState.Open and ConnectionState.Closed (th
remaining members of this enum are reserved for future use and are basically no-ops as of .NET 1.1). Also,
Table of Contents
understand that it is always safe to close a connection whose connection state is currently
C# and the .NET Platform, Second Edition
ConnectionState.Closed.
Introduction
Part One - Introducing C# and the .NET Platform
Connecting to an Access Database
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Much like classic ADO, the process of connecting to an Access database using ADO.NET requires little more
Part Two - The C# Programming Language
than retrofitting your construction string. First, set the Provider segment to the JET engine, rather than
Chapter 3 - C# Language Fundamentals
SQLOLEDB. Beyond this adjustment, set the data source segment to point to the path of your *.mdb file, as
Chapter 4 - Object-Oriented Programming with C# shown here:
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
// Be sure to update the data source segment if necessary!
Chapter 7 - Callback Interfaces, Delegates, and Events
OleDbConnection cn = new OleDbConnection();
Chapter 8 - Advanced C# Type Construction Techniques cn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" +
Part Three - Programming with .NET Assemblies
@"data source = C:\cars.mdb";
Chapter 9 - Understanding .NET Assemblies
cn.Open();
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
PartOnceFourthe- Leveragingconnection thehas.NETbeenLibrariesmade, you can read and manipulate the contents of your data table.
Chapter 12 - Object Serialization and the .NET Remoting Layer
ChapterObtaining13 - BuildingDatabasea Better WindowSchema(IntroduciInformationg Windows F rms)Using Connection Types
Chapter 14 - A Better Painting Framework (GDI+)
ChapterConnection15 - Programmiobjects cangalsowithbeWindowsused toFobtainrms Controlsschema information from a given database through the
GetOleDbSchemaTable() method. As you are most likely aware, "schema information" is little more than
Chapter 16 - The System.IO Namespace
metadata that describes the database itself, rather than the tabular data it is maintaining. The
Chapter 17 - Data Access with ADO.NET
GetOleDbSchemaTable() method takes two parameters, the first of which is a member of the
Part Five - Web Applications and XML Web Services
OleDbSchemaGuid class type. This type exposes a number of read-only fields that map to the sort of metadata
Chapter 18 - ASP.NET Web Pages and Web Controls
you are interested in obtaining (table names, stored procedure names, views, and whatnot).
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
The second parameter is an array of System.Object types that represents what (if any) restrictions should be
Index
placed on the schema information returned by the call. Be aware that the exact format of this array of
List of Figures
System.Object types will vary based on the specified value of the OleDbSchemaGuid parameter. Upon
List of Tables
completion, the GetOleDbSchemaTable() method returns a DataTable type that contains the schema information.
Rather than pound out all of the possible parameter combinations (as they are documented within online help), the following example illustrates how you can make use of an OleDbConnection type to obtain basic schema information regarding each table in the Cars database:
DataTable dtSchemaInfo = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] {null, null, null, "TABLE"});
// Print the DataTable.
for(int curRow = 0; curRow < dtSchemaInfo.Rows.Count; curRow++)
{
for(int curCol= 0; curCol< dtSchemaInfo.Columns.Count; curCol++)
{
Console.Write(dtSchemaInfo.Rows[curRow][curCol].ToString().Trim()+ " "
}
Console.WriteLine();
C# and the .NET Platform, Second Edition
}
by Andrew Troelsen |
ISBN:1590590554 |
Apress © 2003 (1200 pages)
As you will see, itThisis alsocomprehensivepossible to obtaintext startsschemawith detailsa brief overviewfor a specificof thetable in a database using data reader types (more detailsC#tolanguagecome). and then quickly moves to key technical and
architectural issues for .NET developers.
Building a SQL Command via OleDbCommand
Table of Contents
Now that you better understand the role of the OleDbConnection type, the next order of business is to check ou
C# and the .NET Platform, Second Edition
how to submit SQL queries to the database in question. The OleDbCommand type (which as you recall
Introduction
implements the IDbCommand interface) is an OO representation of a SQL query, table name, or stored
procedure that is specified using the CommandType property. This property may take any value from the
Chapter 1 - The Philosophy of .NET
CommandType enum:
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
public enum System.Data.CommandType
Chapter 3 - C# Language Fundamentals
{
Chapter 4 - Object-Oriented Programming with C#
StoredProcedure,
Chapter 5 - Exceptions and Object Lifetime
TableDirect,
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
}
Chapter 8 - Advanced C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
ChapterIf the comma9 - Understantype doesing .indeedNET Assrepresentmblies a SQL query, the CommandText property can be used to get or se
Chapterthe query10 text- Processes,. When creatingAppDomains,an OleDbCommandContexts, Threadstype, you may establish the SQL query as a constructor
Chparameterpter 11 -orTypedirectlyReflection,via theLateCommandTextBindi g, propertyAttribute.-Based Programming
Part Four - Leveraging the .NET Libraries
Also, when you are creating an OleDbCommand type, you will need to specify the OleDbConnection to be used
Chapter 12 - Object Serialization and the .NET Remoting Layer
Again, you may do so as a constructor parameter or via the Connection property. To illustrate, ponder the two
Chapter 13 - Building a Better Window (Introducing Windows Forms)
(functionally equivalent) ways to build an OleDbCommand type that associates a SQL SELECT statement with
Chapter 14 - A Better Painting Framework (GDI+) an OleDbConnection variable named "cn":
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
// Specify SQL command and connection as constructor parameters.
Chapter 17 - Data Access with ADO.NET
string strSQL1 = "Select Make from Inventory where Color='Red'";
Part Five - Web Applications and XML Web Services
OleDbCommand myCommand1 = new OleDbCommand(strSQL1, cn);
Chapter 18 - ASP.NET Web Pages and Web Controls
// Specify SQL command and connection via properties.
Chapter 19 - ASP.NET Web Applications
string strSQL2 = "Select Make from Inventory where Color='Red'";
Chapter 20 - XML Web Services
OleDbCommand myCommand2 = new OleDbCommand();
Index
myCommand.Connection = cn;
List of Figures
myCommand.CommandText = strSQL2;
List of Tables
Realize that at this point, you have not literally submitted the SQL query to the Cars database, but rather prepp the state of the command type for future use. Table 17-15 highlights some additional members of the OleDbCommand type.
Table 17-15: Members of the OleDbCommand Type
|
|
|
|
|
|
|
OleDbCommandC# and theMeaning.NET Platform,in LifeSecond Edition |
||||
|
Member |
by Andrew Troelsen |
ISBN:1590590554 |
||
|
|
|
|
|
|
|
|
Apress © |
|
2003 (1200 pages) |
|
|
CommandText |
|
Gets or sets the SQL command text or the provider-specific syntax to run |
||
|
|
This comprehensive text starts with a brief overview of the |
|||
|
|
|
|
against the data source. |
|
|
|
C# language and then quickly moves to key technical and |
|||
|
|
architectural |
|
issues for .NET developers. |
|
|
CommandTimeout |
|
Gets or sets the time to wait while executing the command before terminating |
||
|
|
|
|
the attempt and generating an error. The default is 30 seconds. |
|
|
|
|
|
|
|
Table of |
tents |
|
Gets or sets how the CommandText property is interpreted via the |
||
|
CommandType |
|
|||
C# and the .NET Platform, |
SecondCommandTypeEdition |
enumeration. The default value is CommandType.Text, which |
|||
Introduction |
|
represents a SQL query. |
|||
|
|
|
|
||
Part One - Introducing C# and the .NET Platform |
|||||
|
Connection |
|
Gets or sets the OleDbConnection used by this instance of the |
||
Chapter 1 |
- The Philosophy of .NET |
|
|||
|
|
|
|
OleDbCommand. |
|
|
Chapter 2 |
- Building C# |
|
Applications |
|
PartParametersTwo - The C# ProgrammingGetsLanguagethe collection of OleDbParameter types used for a parameterized query.
|
Chapter 3 |
- C# Language |
|
Fundamentals |
|
|
Cancel() |
|
|
Cancels the execution of a command. |
|
|
Chapter 4 |
- Object-Oriented |
|
Programming with C# |
|
|
|
|
|||
|
ChapterExecuteReader()5 - Exceptions and |
|
ObjectReturnsLifetimean instance of an OleDbDataReader, which provides forward-only, |
|
|
|
Chapter 6 |
- Interfaces and |
|
Collectionsread-only access to the underlying data. |
|
|
|
|
|
|
|
|
Chapter 7 |
- Callback Interfaces, Delegates, and Events |
|
||
|
ExecuteNonQuery() |
|
This method issues the command text to the data store, without returning an |
|
|
|
Chapter 8 |
- Advanced C# Type Construction Techniques |
|
||
|
|
|
|
OleDbDataReader type. |
|
Part Three - Programming with .NET Assemblies |
|
||||
|
|||||
|
ExecuteScalar() |
|
A lightweight version of the ExecuteNonQuery() method, designed specifically |
|
|
Chapter 9 |
- Understanding .NET Assemblies |
|
|||
|
|
|
|
for singleton queries (such as obtaining a record count). |
|
|
Chapter 10 |
- Processes, AppDomains, Contexts, and Threads |
|
||
|
|||||
|
|
|
|
|
|
Chapter 11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
|
|||
|
Prepare() |
|
Creates a prepared (or compiled) version of the command on the data |
|
Part Four - Leveraging the .NETsourceLibraries.
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables