Is this the correct way to use the current databaseconnection

Is this the correct way to use the current databaseconnection

  
Hi,

we used to have the "DBTransactionManager.Current.ConnectionString;"

so I am wondering if this the right way to do it in P9:


        void bulkCopyNew(DataTable dt)
        {
            //get the current transaction
            RequestTransaction rt = OutSystems.RuntimePublic.Db.DatabaseAccess.ForRunningApplication().GetRequestTransaction();
            using (SqlConnection cn = (SqlConnection)rt.GetConnection().GetDriverConnection())
            {
                if (cn.State == ConnectionState.Open)
                {
                    using (SqlBulkCopy copy = new SqlBulkCopy(cn,SqlBulkCopyOptions.Default, (SqlTransaction)rt.GetDriverTransaction()))
                    {
                        copy.BulkCopyTimeout = 30;
                        copy.WriteToServer(dt);
                    } // end using
                }
            }
        }
Thanks in advance!
Hi J,

You should avoid applying the using statement to the database connection object since it will end up closing the conection, thus, making it unusable for the rest of the request! Instead, you should apply the using statement to the request transaction.

You can check the RuntimePublic.Db API documentation on information how to use this, for instance in the Insert Data in the Application Database example.

Thanks,

Ricardo Neto