using Npgsql; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using WWPipeLine.Commons; namespace WWPipeLine.MapBasic { public class NpgsqlHelper { public NpgsqlHelper() { } public static string ConStr = ComsStatic.ConfigsNpgsql; public int ExecuteNonQuery(string sql) { int result = -1; using (NpgsqlConnection connection = new NpgsqlConnection(ConStr)) { using (NpgsqlCommand command = new NpgsqlCommand(sql, connection)) { try { connection.Open(); result = command.ExecuteNonQuery(); } catch (NpgsqlException exception) { LogHelper.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message)); } finally { connection.Close(); } } } return result; } public object ExecuteScalar(string sql) { object obj = null; using (NpgsqlConnection connection = new NpgsqlConnection(ConStr)) { using (NpgsqlCommand command = new NpgsqlCommand(sql, connection)) { try { connection.Open(); obj = command.ExecuteScalar(); } catch (NpgsqlException exception) { obj = -1; LogHelper.Error(string.Format("执行SQL[{0}]出错,详细信息为:{1}", sql, exception.Message)); } finally { connection.Close(); } } } return obj; } public DataSet ExecuteQuery(string sql) { DataSet result = new DataSet(); using (NpgsqlConnection connection = new NpgsqlConnection(ConStr)) { using (NpgsqlDataAdapter dataAdapter = new NpgsqlDataAdapter(sql, connection)) { try { connection.Open(); dataAdapter.Fill(result); } catch (NpgsqlException exception) { LogHelper.Error(string.Format("执行SQL【{0}】出错,详细信息为:{1}", sql, exception.Message)); } finally { connection.Close(); } } } return result; } } }