Sample DB2 Everyplace .NET Data Provider application code for WinCE and Win32

There are two sample applications that illustrate how to develop applications for WinCE and Win32 using the DB2 Everyplace .NET Data Provider:

Both files are located in:

The following is an example of one of the sample applications.

using System;
using System.Text;
using System.Data;
 
using IBM.Data.DB2.DB2e;
 
/* 
 * Sample1
 * 
 * The following example creates a table, insert some rows to it, fetches
 * all the rows from the table, and finally drops the table.
 * 
 */ 
namespace IBM.Data.DB2.DB2e.Samples
{
        class DB2eSample1
        {
                /// <summary>
 
 
 
     	/// The main entry point for the application.
    	/// </summary>
     [STAThread]
     static void Main(string[] args)
       {
         DB2eConnection conn   = null;
         DB2eCommand cmd       = null;
         DB2eDataReader reader = null;
         	String connString     = @"database=.\; uid=user1; pwd=user1";
        		int rowsAffected      = 0;
 
        			try 
                {
                 conn = new DB2eConnection(connString);
                 conn.Open();
                
                   Console.WriteLine("creating table t1...");
                   cmd = new DB2eCommand("create table t1 (c1 int primary key not null,
								c2 smallint, c3 char(10), c4 varchar(10), c5 decimal(8,2), c6 date,
								c7 time, c8 timestamp )", conn);
                      rowsAffected = cmd.ExecuteNonQuery();
                      Console.WriteLine("inserting a row into table t1...");
                      cmd.CommandText = "insert into t1 values (1, 10, 'John',
									'Yip', null, current date,	current time, current timestamp)";
                  rowsAffected = cmd.ExecuteNonQuery();
                  Console.WriteLine("inserting a row into table t1...");
                  cmd.CommandText = "insert into t1 values (2, 20, 'Mary', 'Jann',
									2.2, current date, current time, current timestamp)";
                  rowsAffected = cmd.ExecuteNonQuery();
                  cmd.CommandText = "select * from t1";
                  Console.WriteLine("fetching resultset from table t1...");
                  reader = cmd.ExecuteReader();
                      while (reader.Read())
                        {
                          if (!reader.IsDBNull(0)) 
 	                           Console.Write(reader.GetInt32(0) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(1))
                              Console.Write(reader.GetInt16(1) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(2)) 
                              Console.Write(reader.GetString(2) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(3))
                              Console.Write(reader.GetString(3) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(4))
                              Console.Write(reader.GetDecimal(4) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(5))
                              Console.Write(reader.GetDate(5) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(6))
                              Console.Write(reader.GetTime(6) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                          if (!reader.IsDBNull(7))
                              Console.Write(reader.GetDateTime(7) + "\t");
                          else
                              Console.Write("NULL " + "\t");
                              Console.WriteLine();
                          }
                     reader.Close();
                     reader = null;
                     Console.WriteLine("dropping table t1...");
                     cmd.CommandText = "drop table t1";
                     cmd.ExecuteNonQuery();
                   }
                     catch (DB2eException e1) 
 		                 {
                         int cnt = e1.Errors.Count;
                         for (int i=0; i < cnt; i++)
                           {
                             Console.WriteLine("Error #" + i + "\n" +
                                "Message: " + e1.Errors[i].Message + "\n" +
                                "Native: " + e1.Errors[i].NativeError.ToString() + "\n" +
                                "SQL: " + e1.Errors[i].SQLState + "\n");
                           }
                        }
                     catch (Exception ex) 
                       {
                             Console.WriteLine(ex.Message);
                       }
                     finally 
                        {
                             if (reader != null)
                                {
                                   reader.Close();
                                   reader = null;
                                }
                             if (conn != null) 
                                {
                                   conn.Close();
                                   conn = null;
                                }
                        }
                }  // end of Main
 
        }  // end of class
 
} // end of namespace

Related concepts

Related tasks