Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Friday, September 01, 2006

Examples and Samples for ADO.NET in Cloudscape

The following example code fragments illustrate how to access a Cloudscape/Derby database using the ODBC .NET Data Provider. A full C# example is provided in the Download section below. To compile the full C# example code, install the .NET Framework v1.1 SDK and use the C# compiler, csc.exe. For example, the following compiles a C# source file called Sample1.cs and creates a console application named Sample1.exe:

csc /target:exe Sample1.cs

Listing 1 shows how to establish a connection to a database called "SAMPLEDB" using "abc" as both the user id and password.

Listing 1: Making a connection
String connString = "DSN=SAMPLEDB;UID=abc;PWD=abc;";// where datasource name is SAMPLEDB OdbcConnection conn = new OdbcConnection(connString); conn.Open();

Listing 2 shows how to start, execute, and commit a transaction.

Listing 2: Executing Statements And Using Transactions
// conn is an OdbcConnection instanceOdbcCommand cmd = conn.CreateCommand(); OdbcTransaction trans = conn.BeginTransaction();  // start a transactioncmd.Transaction = trans;cmd.CommandText = "UPDATE staff " + " SET salary = (SELECT MIN(salary) " +            " FROM staff " + " WHERE id >= 310) " + " WHERE id = 310"; cmd.ExecuteNonQuery();  // execute the statementtrans.Commit();	   // commit the transaction

Listing 3 shows how to call a stored procedure

Listing 4 reads the results of a query.

Listing 5: C# example for ADO.NET in Cloudscape

Post a Comment