1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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() { }
- private static string ConStr = IniHelper.Read(IniHelper.KEY_ConfigsNpgsql_PATH);
- 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)
- {
- 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;
- }
- }
- }
|