Parametrized Queries vs. Table-direct mode (SQL Server CE)

Share

            Recently, I worked on a mobile device application and I had to insert about 50000 rows into a SQL Server CE database. NET Compact Framework offers some ways to complete this task including using of DataAdapter and DataSet objects, parametrized queries or SqlCeResultSet(table-direct mode). Considering that the first way is the slowest I will not talk about it.

     Parametrized queries stores a SQL statement, that can be executed with more then one set of values.

 SqlCeConnection conn = new SqlCeConnection("your connection string");
 SqlCeCommand cmd = new SqlCeCommand();
 cmd.Connection = conn;
 cmd.CommandText = "INSERT INTO TableName (FirstColumn, SecondColumn) VALUES (@FirstColumnParam, @SecondColumnParam)"; 
 // declaring DbType for SqlCeParameter will slightly improve performance
 cmd.Parameters.Add(new SqlCeParameter()) ;
 cmd.Parameters[0].DbType = DbType.Int32;
 cmd.Parameters.Add(new SqlCeParameter()) ;
 cmd.Parameters[1].DbType = DbType.String;
 cmd.Parameters[1].Size = 20 ;
 cmd.Prepare() ; // Create a compiled version of command
 cmd.Connection.Open();
 while(condition)
 {
  cmd.Parameters[0].Value = 1;
  cmd.Parameters[1].Value = "test data";
  cmd.ExecuteNonQuery();
 }
 cmd.Connection.Close();

     Table-direct mode is a mechanism that work directly against the underlying table structure.

 SqlCeConnection conn = new SqlCeConnection("your connection string");
 conn.Open();
 SqlCeCommand cmd = conn.CreateCommand();
 cmd.CommandType = System.Data.CommandType.TableDirect;
 cmd.CommandText = "you_table_name";
 SqlCeResultSet rs =  cmd.ExecuteResultSet(ResultSetOptions.Updatable);
 SqlCeUpdatableRecord rec = rs.CreateRecord();
 while(condition)
 {
  rec.SetInt32(0, value);
  rec.SetString(1, value);
  rs.Insert(rec);
 }
 rs.Close();
 conn.Close();

        After testing both mechanisms I realized that for a small number of operations the parametrized query is almost as fast as the table-direct mode, but for a large number of operations the table-direct mode is definitely the fastest mechanism. Another mechanism for inserting a large number of data into a SQL Server CE database is SqlCeBulkCopy object based on table-direct mode.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close