static void Insert(PgSqlConnection connection) { //insert PgSqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO public._gps_log (_id,_uid,_status,_time,_validity,_lat,_lon,_speed,_course,_distance,_judgement,_or_lon,_or_lat,_satellites,_temperature,_voltage) VALUES (1681185,'_uid',2,'2012-10-01 18:32:50.553+08','a',25.062923,121.522705,0,0,0,0,0,0,3,35,100)"; //cmd.CommandText = "INSERT INTO public.test (id) VALUES (1)"; //async IAsyncResult cres = command.BeginExecuteNonQuery(null, null); 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."); */ }
//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); } }
//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)"; //async IAsyncResult cres = command.BeginExecuteNonQuery(null, null); 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."); * */ return(true); } else { return(false); } } catch (PgSqlException ex) { Console.WriteLine("Exception occurs: {0}", ex.Error); return(false); } }
//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); } }