ComToolkit.Data

Connection string

In this example a Sql Server connection is used.
string connectionString = @"Provider=SQLOLEDB;Data Source=servername\sqlexpress;Initial Catalog=databasename;User ID=username; Password=password";
connectionstrings.com is a great reference.

Command samples

ExecuteNonQuery()

using (var connection = new ComToolkit.Data.AdoConnection(connectionString))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "UPDATE MyTable SET MyColumn = 'new value' WHERE Id = 1";
    int recordsAffected = command.ExecuteNonQuery();
}

ExecuteScalar()

using (var connection = new ComToolkit.Data.AdoConnection(connectionString))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT COUNT(*) FROM MyTable";
    int count = (int)command.ExecuteScalar();
}

ExecuteReader()

using (var connection = new ComToolkit.Data.AdoConnection(connectionString))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT MyColumn FROM MyTable";
    var reader = command.ExecuteReader();
    while (reader.Read())
    {
        object byindex = reader[0];
        object bystring = reader["MyColumn"];
        //to use dynamic properties reader must be declared as dynamic.
        //object bydynamic = reader.MyColumn;
    }
 }

Asynchronous Command samples

ExecuteNonQueryAsync()

private ComToolkit.Data.AdoConnection connection;
private void ExecuteNonQueryAsyncSample()
{
    connection = new ComToolkit.Data.AdoConnection(connectionString);
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "UPDATE MyTable SET MyColumn = 'new value' WHERE Id = 1";
    command.ExecuteNonQueryCompleted += new EventHandler<ComToolkit.Data.ExecuteNonQueryCompletedEventArgs>(command_ExecuteNonQueryCompleted);
    command.ExecuteNonQueryAsync();
}

void command_ExecuteNonQueryCompleted(object sender, ComToolkit.Data.ExecuteNonQueryCompletedEventArgs e)
{
    //TODO:process result
}

ExecuteScalarAsync()

private ComToolkit.Data.AdoConnection connection;
private void ExecuteScalarAsyncSample()
{
    connection = new ComToolkit.Data.AdoConnection(connectionString);
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT COUNT(*) FROM MyTable";
    command.ExecuteScalarCompleted += new EventHandler<ComToolkit.Data.ExecuteScalarCompletedEventArgs>(command_ExecuteScalarCompleted);
    command.ExecuteScalarAsync();
}

void command_ExecuteScalarCompleted(object sender, ComToolkit.Data.ExecuteScalarCompletedEventArgs e)
{
    //TODO:process result
}

ExecuteReaderAsync()

private ComToolkit.Data.AdoConnection connection;
private void ExecuteReaderAsyncSample()
{
    connection = new ComToolkit.Data.AdoConnection(connectionString);
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT MyColumn FROM MyTable";
    command.ExecuteReaderCompleted += new EventHandler<ComToolkit.Data.ExecuteReaderCompletedEventArgs>(command_ExecuteReaderCompleted);
    command.ExecuteReaderAsync();
}

void command_ExecuteReaderCompleted(object sender, ComToolkit.Data.ExecuteReaderCompletedEventArgs e)
{
    //TODO:process result
}

Parameters

Highly experimental
  • Only few data types has been implemented for use with parameters, when i got more time i will implement the rest.

using (var connection = new ComToolkit.Data.AdoConnection(connectionString))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT MyColumn FROM MyTable WHERE Id = ?";
    command.Parameters.AddWithValue("@Id", "ABC123");
    var reader = command.ExecuteReader();
    while (reader.Read())
    {
        //TODO:process result
    }
}

Batch commands

Restrictions

  • Every statement needs to be terminated using GO or ;.
  • Very basic SQL parser so GO and ; will be reserved and cannot be used in any other part of the statement.
  • Last statement will be considered the result (where a AdoDataReader or object is expected).

Sample batch command

@"
/*Comments
like these*/
//will be
--ignored
UPDATE mytable SET mycolumn='new value' WHERE id = 1
GO
UPDATE mytable SET mycolumn='new value2' WHERE id = 2;
SELECT * FROM mytable;"

Last edited Jan 15, 2010 at 5:51 PM by mlarsson, version 3

Comments

deanforu44 Aug 25, 2014 at 1:59 PM 
where can I find the version that works with silverlight 5?

kunaladesai Nov 5, 2011 at 1:55 PM 
Does this work with Vb# 2010 , vb code , i am not c# user