public IEnumerable <int> Execute(params SqlCommand[] sqlCommands)
        {
            using (var connection = new PgSqlConnection(_connectionString))
            {
                var allRecordsAffected = new List <int>();
                connection.Open();
                connection.BeginTransaction();
                try
                {
                    sqlCommands.ToList().ForEach(sqlCommand =>
                    {
                        var recordsAffected = connection.Execute(sqlCommand.Sql, sqlCommand.Param);
                        allRecordsAffected.Add(recordsAffected);
                    });

                    connection.Commit();
                }
                catch
                {
                    connection.Rollback();
                    throw;
                }
                finally
                {
                    connection.Close();
                }

                return(allRecordsAffected);
            }
        }
Ejemplo n.º 2
0
        //For UPDATE, INSERT, and DELETE statements
        public bool modify(string cmd)
        {
            try
            {
                if (pgSqlConnection != null && IsConnected)
                {
                    //insert
                    PgSqlCommand command = pgSqlConnection.CreateCommand();
                    command.CommandText = cmd;
                    //cmd.CommandText = "INSERT INTO public.test (id) VALUES (1)";
                    pgSqlConnection.BeginTransaction();
                    //async
                    IAsyncResult cres = command.BeginExecuteNonQuery(null, null);
                    Console.Write("In progress...");
                    while (!cres.IsCompleted)
                    {
                        Console.Write(".");
                        //Perform here any operation you need
                    }

                    /*
                     * if (cres.IsCompleted)
                     *  Console.WriteLine("Completed.");
                     * else
                     *  Console.WriteLine("Have to wait for operation to complete...");
                     */
                    int RowsAffected = command.EndExecuteNonQuery(cres);
                    //Console.WriteLine("Done. Rows affected: " + RowsAffected.ToString());

                    /*
                     * //sync
                     * int aff = cmd.ExecuteNonQuery();
                     * Console.WriteLine(aff + " rows were affected.");
                     *
                     */
                    pgSqlConnection.Commit();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (PgSqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Modify exception occurs: {0}" + Environment.NewLine + "{1}", ex.Error, cmd);
                log.Error("Modify exception occurs: " + Environment.NewLine + ex.Error + Environment.NewLine + cmd);
                Console.ResetColor();
                pgSqlConnection.Rollback();
                return(false);
            }
        }
Ejemplo n.º 3
0
        //For UPDATE, INSERT, and DELETE statements
        public bool modify(string cmd)
        {
            Stopwatch    stopWatch = new Stopwatch();
            PgSqlCommand command   = null;

            stopWatch.Start();
            try
            {
                if (pgSqlConnection != null && IsConnected)
                {
                    //insert
                    command             = pgSqlConnection.CreateCommand();
                    command.CommandText = cmd;
                    //cmd.CommandText = "INSERT INTO public.test (id) VALUES (1)";
                    pgSqlConnection.BeginTransaction();
                    //async
                    IAsyncResult cres = command.BeginExecuteNonQuery(null, null);
                    //Console.Write("In progress...");
                    //while (!cres.IsCompleted)
                    {
                        //Console.Write(".");
                        //Perform here any operation you need
                    }

                    /*
                     * if (cres.IsCompleted)
                     *  Console.WriteLine("Completed.");
                     * else
                     *  Console.WriteLine("Have to wait for operation to complete...");
                     */
                    int RowsAffected = command.EndExecuteNonQuery(cres);
                    //Console.WriteLine("Done. Rows affected: " + RowsAffected.ToString());

                    /*
                     * //sync
                     * int aff = cmd.ExecuteNonQuery();
                     * Console.WriteLine(aff + " rows were affected.");
                     *
                     */
                    pgSqlConnection.Commit();
                    ThreadPool.QueueUserWorkItem(callback =>
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine(
                            "S++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                        Console.WriteLine("sql Write:\r\n" + cmd);
                        Console.WriteLine(
                            "E++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                        Console.ResetColor();
                    });
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    command = null;
                    stopWatch.Stop();
                    // Get the elapsed time as a TimeSpan value.
                    TimeSpan ts = stopWatch.Elapsed;

                    // Format and display the TimeSpan value.
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                       ts.Hours, ts.Minutes, ts.Seconds,
                                                       ts.Milliseconds / 10);
                    SiAuto.Main.AddCheckpoint(Level.Debug, "sql modify take time:" + elapsedTime, cmd);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (PgSqlException ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Modify exception occurs: {0}" + Environment.NewLine + "{1}", ex.Error, cmd);
                log.Error("Modify exception occurs: " + Environment.NewLine + ex.Error + Environment.NewLine + cmd);
                Console.ResetColor();
                pgSqlConnection.Rollback();
                if (command != null)
                {
                    command.Dispose();
                }
                command = null;
                return(false);
            }
        }