EXECUTING SQL CODE FROM AN APPLICATION

The example deals with the simple scenario of executing some SQL code from an application — to generate some default data, for instance. You may find code like this in many applications:

image

static void FillDatabase( ) {

  using(var conn = new SqlCeConnection(DBCONNSTR)) {

    conn.Open( );

    try {

      using (var trans = conn.BeginTransaction( )) {

        ExecuteSQL(trans, "create table people(id int, name ntext)");

        trans.Commit( );

      }

 

      using (var trans = conn.BeginTransaction( )) {

        ExecuteSQL(trans, "insert into people(id, name) values(1, 'Harry')");

        ExecuteSQL(trans, "insert into people(id, name) values(2, 'Jane')");

        ExecuteSQL(trans, "insert into people(id, name) values(3, 'Willy')");

        ExecuteSQL(trans, "insert into people(id, name) values(4, 'Susan')");

        ExecuteSQL(trans, "insert into people(id, name) values(5, 'Bill')");

        ExecuteSQL(trans, "insert into people(id, name) values(6, 'Jennifer')");

        ExecuteSQL(trans, "insert into people(id, name) values(7, 'John')");

        ExecuteSQL(trans, "insert into people(id, name) values(8, 'Anna')");

        ExecuteSQL(trans, "insert into people(id, name) values(9, 'Bob')");

        ExecuteSQL(trans, "insert into people(id, name) values(10, 'Mary')");

 

        trans.Commit( );

      }

    }

    finally {

      conn.Close( ); ...

Get Functional Programming in C#: Classic Programming Techniques for Modern Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.