Showing posts with label Group of Queries execute in single Transaction. Show all posts
Showing posts with label Group of Queries execute in single Transaction. Show all posts


SQLCE
Queries protection from injection
Group of Queries execute in single Transaction
---------------------------------------------------------------------


private void button1_Click(object sender, EventArgs e)
        {

            SqlCeCommand[] cmd = new SqlCeCommand[500];
            for (int i = 0; i <3; i++)
            {               
                cmd[i] = new SqlCeCommand();               
                cmd[i].CommandText = "insert into kart(name, city)values(@name,@city)";
                cmd[i].Parameters.AddWithValue("@name", name_txt.Text);
                cmd[i].Parameters.AddWithValue("@city", city_txt.Text);              
            }
            arrycmd(cmd);
        }


        void arrycmd(SqlCeCommand[] cmd_array)
        {
            //connection check and open
            if (con.State == ConnectionState.Open) { con.Close(); } con.Open();
                       
            //create transaction
            SqlCeTransaction transaction;
            transaction = con.BeginTransaction(IsolationLevel.ReadCommitted);
          
            int m = cmd_array.Length;
            try
            {
                for (int i = 0; i < m; i++)
                {
                    if (cmd_array[i] == null)
                        break;
                    // Assign connection  and transaction to the current command
                    cmd_array[i].Connection = con;
                    cmd_array[i].Transaction = transaction;
                    cmd_array[i].ExecuteNonQuery();

                    cmd_array[i].Dispose();
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
            }

            con.Close();
            MessageBox.Show("done");
        }
      

    }



Your IP Address is:

Browser: