Using VistaDB with ADO.NET - Common Operations using Any ADO.NET Provider
In This Topic
The following code examples show how to perform common data operations using ADO.NET in a provider neutral matter. This means you can switch between SQL Server and VistaDB (or even other databases) just by changing the provider in the connection string. Follow the instructions in ADO.NET Factory Objects in your app.config or web.config to set up your application to use factory objects.
As you read these examples you'll see a lot of repetitive code. In your application you can dramatically reduce this by creating a function for getting a connection (with your connection string and the current provider) and for creating parameters. At that point the code is about the same number of lines as provider-specific code.
Connecting To A Database
This code sample shows the basic step of opening a VistaDB database.
Creating A Command
Most database operations require creating a DbCommand object, configuring it for your operation, and associating it with a database connection to work with. This example shows creating the connection and command and associating them together.
Inserting Data Using a Command
In this example the command is being used to execute a simple TSQL insert. Since no data set is expected in return the ExecuteNonQuery() method is used to execute the command.
Inserting Data Using a Parameterized Command
In this example the command is being used to execute a simple TSQL insert. It improves on the previous example by using a parameterized TSQL statement and ADO.NET Parameters to pass data. It is highly recommended that parameters always be used to pass data from your application to ADO.NET for reliability, consistency, and to ensure there is no possibility of a SQL Injection Attack against your code.
Querying Data and Filling a Data Table
To retrieve multiple rows of data from a database ADO.NET uses a TableAdapter to fill a DataTable. The DataTable can have exactly one set of rows with a common schema.
Calling Stored Procedures
Like Microsoft SQL Server, VistaDB supports Stored Procedures. In this example the stored procedure "CalcSalesTax" is being called with several parameters. To prevent SQL Injection attacks parameterized commands should be used to pass data to stored procedures instead of attempting to assemble the query as a string.
See Also