Tips and Tricks in a world of Mix

When you need a test running during your process to insert data you may want to use task for async insertion.

Also you may run it parallel to your process from outside.

If you want to be able to stop you should implement Cancellation Token and put it inside the process as a stop condition for a while loop.

 


namespace AsyncTest
{
 public class InitTestPerSecond{
 public Task initData;
 public InitTestPerSecond()
 {
 initData = new Task()(
 ()=>
 {
 while(true)
 {
 MyDAL.InsertInTableEachSecond(DateTime.Now);
 Thread.Sleep(1000);
 }
 });
 initData.Start();
 }
}
 
namespace MyDAL
{
 public class MyDAL{
 public static void InsertInTableEachSecond(DateTime timestamp)
 {
 try{
 OracleConnection localConn = new OracleConnection(Utils.GetConnectionString());
 localConn.Open();
 int maxId;
 using (OracleCommand maxIdCommand = new OracleCommand("SELECT MAX(MYID) FROM SCHEMA_NAME.MYTABLE", localConn))
 {
 maxId = int.Parse(maxIdCommand.ExecuteScalar().ToString());
 }
 using (OracleCommand initMyData = localConn.CreateCommand())
 {
 
 initMyData = string.Format(" INSERT INTO SCHEMA_NAME.MYTABLE (MYID , COL1, DATE)
 VALUES 
 ({0}, 
 '{0} test ' ,
 {1})", ++maxId ,Utils.GetDateFormat(timeStamp) ));
 
 initMyData.ExecuteNonQuery();
 }
 localConn.Close();
 localConn.Dispose();
 }
 catch(Exception ex){
 throw ex;
 }
 }
 
 }
 
}
 
public class Utils{
 
 public static string GetDateFormat(DateTime date){
 return string.Format(@"TO_DATE('{0}', 'MM/DD/YYYY HH24:MI:SS')", date.ToString("MM/dd/YYYY HH:mm:ss"));
 }
 
 public static string GetConnectionString()
 {
 return ConnectionStringHandler.ConnectionString;
 }
} 
 
 
public static class ConnectionStringHandler
{
 public static string ConnectionString= String.Empty;
 static ConnectionStringHandler()
 {
 Decrypt();
 ConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
 Encrypt();
 }
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: