本文整理汇总了C#中Npgsql.NpgsqlConnection.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlConnection.Dispose方法的具体用法?C# NpgsqlConnection.Dispose怎么用?C# NpgsqlConnection.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Npgsql.NpgsqlConnection
的用法示例。
在下文中一共展示了NpgsqlConnection.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Empty
public void Empty()
{
var connection = new NpgsqlConnection(ConnectionString);
try
{
connection.Open();
foreach (var tablename in StoreInfo.Tables)
{
using (var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = [email protected]"delete from {tablename};";
command.ExecuteNonQuery();
}
}
}
catch (NpgsqlException exception)
{
throw new Exception($"Could not delete all from table {StoreInfo.Name}; see the inner exception for more information.", exception);
}
finally
{
connection.Dispose();
}
}
示例2: Connection_Test
public static bool Connection_Test(ENTITE.Serveur bean)
{
if (bean.Control_())
{
NpgsqlConnection con = new NpgsqlConnection();
try
{
string constr = "PORT=" + bean.getPort + ";TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;COMPATIBLE= 2.0.14.3;DATABASE=" + bean.getDatabase + ";HOST=" + bean.getAdresse + ";PASSWORD=" + bean.getPassword + ";USER ID=" + bean.getUser + "";
con = new NpgsqlConnection(constr);
con.Open();
return true;
}
catch (NpgsqlException ex)
{
Messages.Exception(ex);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
return false;
}
示例3: Destroy
public void Destroy()
{
var connection = new NpgsqlConnection(_connectionString);
try
{
connection.Open();
foreach (var tablename in StoreInfo.Tables)
{
using (var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = String.Format(@"drop table {0};", tablename);
command.ExecuteNonQuery();
}
}
}
catch (NpgsqlException exception)
{
throw new Exception(String.Format("Could not drop table {0}; see the inner exception for more information.", _storeInfo.Name), exception);
}
finally
{
connection.Dispose();
}
}
示例4: ExecuteScript
private static void ExecuteScript(string filepath)
{
NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["Postgres"].ToString());
FileInfo file = new FileInfo(filepath);
string script = file.OpenText().ReadToEnd();
NpgsqlCommand cmd = new NpgsqlCommand(script, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
示例5: PgMalDataLoader
public PgMalDataLoader(string connectionString)
{
m_conn = new NpgsqlConnection(connectionString);
try
{
Logging.Log.Debug("Connecting to PostgreSQL.");
m_conn.Open();
Logging.Log.Debug("Connected to PostgreSQL.");
}
catch
{
m_conn.Dispose();
throw;
}
}
示例6: ConnectWithPool
public void ConnectWithPool()
{
NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder(Conn.ConnectionString);
csb.Pooling = true;
String conStr = csb.ConnectionString;
using (var metrics = TestMetrics.Start(TestRunTime, true))
{
while (!metrics.TimesUp)
{
var con = new NpgsqlConnection(conStr);
con.Open();
con.Dispose();
metrics.IncrementIterations();
}
}
}
示例7: CheckHealth
/// <summary>
/// Check that a connection can be established to Postgresql and return the server version
/// </summary>
/// <param name="connectionString">An Npgsql connection string</param>
/// <returns>A <see cref="HealthResponse"/> object that contains the return status of this health check</returns>
public static HealthResponse CheckHealth(string connectionString)
{
try
{
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();
string host = conn.Host;
string version = conn.PostgreSqlVersion.ToString();
int port = conn.Port;
conn.Close();
conn.Dispose();
return HealthResponse.Healthy(new { host = host, port = port , version = version});
}
catch (Exception e)
{
return HealthResponse.Unhealthy(e);
}
}
示例8: Deconnection
public static void Deconnection(NpgsqlConnection con)
{
if (con != null)
{
try
{
con.Close();
con.Dispose();
}
catch (NpgsqlException ex)
{
Messages.Exception(ex);
}
finally
{
con = null;
}
}
}
示例9: GlobalRegistration
public void GlobalRegistration()
{
NpgsqlConnection.RegisterEnumGlobally<Mood>();
var myconn = new NpgsqlConnection(ConnectionString);
myconn.Open();
const Mood expected = Mood.Ok;
var cmd = new NpgsqlCommand("SELECT @p::MOOD", myconn);
var p = new NpgsqlParameter { ParameterName = "p", Value = expected };
cmd.Parameters.Add(p);
var reader = cmd.ExecuteReader();
reader.Read();
Assert.That(reader.GetFieldType(0), Is.EqualTo(typeof(Mood)));
Assert.That(reader.GetFieldValue<Mood>(0), Is.EqualTo(expected));
Assert.That(reader.GetValue(0), Is.EqualTo(expected));
reader.Close();
cmd.Dispose();
myconn.Dispose();
}
示例10: GetConnectionState
public void GetConnectionState()
{
// Test created to PR #164
NpgsqlConnection c = new NpgsqlConnection();
c.Dispose();
Assert.AreEqual(ConnectionState.Closed, c.State);
}
示例11: ConnectionCreate
/// <summary>
///
/// </summary>
/// <param name="db"></param>
/// <param name="cn"></param>
/// <param name="com"></param>
private static void ConnectionCreate(string db, out NpgsqlConnection cn, out NpgsqlCommand com)
{
Npgsql_Class npgsql_class = new Npgsql_Class();
String connectionString = npgsql_class.connectionstring(db);
cn = new NpgsqlConnection(connectionString);
if (cn.State.ToString() == "Open")
{
cn.Close();
cn.Dispose();
}
cn.Open();
com = cn.CreateCommand();
}
示例12: Connect
private void Connect ()
{
try {
string connectionString = String.Format("Server={0};Database={1};User ID={2};Password={3};",
Configuration.DatabaseHost,
Configuration.DatabaseName,
Configuration.DatabaseUser,
Configuration.DatabasePassword
);
if (Configuration.DatabasePort != 0)
connectionString += string.Format ("Port={0};", Configuration.DatabasePort);
dbcon = new NpgsqlConnection (connectionString);
log.DebugFormat ("Connecting to database, connection string: {0}", connectionString);
dbcon.Open ();
object db_now_obj = ExecuteScalar ("SELECT now();");
DateTime db_now;
DateTime machine_now = DateTime.Now;
db_now = (DateTime) db_now_obj;
db_time_difference = db_now - machine_now;
log.DebugFormat ("DB now: {0:yyyy/MM/dd HH:mm:ss.ffff}, current machine's now: {1:yyyy/MM/dd HH:mm:ss.ffff}, adjusted now: {3}, diff: {2:yyyy/MM/dd HH:mm:ss.ffff} ms", db_now, machine_now, db_time_difference.TotalMilliseconds, Now);
} catch (Exception) {
if (dbcon != null) {
dbcon.Dispose ();
dbcon = null;
}
throw;
}
}
示例13: lock
PostgresTableMetadata IPostgresStore.GetOrCreateTable(Type type)
{
lock (_lock)
{
if (_tables.ContainsKey(type))
{
return _tables[type];
}
var table = new PostgresTableMetadata(type);
var connection = new NpgsqlConnection(_connectionString);
try
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = String.Format(@"
CREATE TABLE IF NOT EXISTS public.{0}
(
id uuid NOT NULL DEFAULT md5(random()::text || clock_timestamp()::text)::uuid,
body json NOT NULL,
created timestamp without time zone NOT NULL DEFAULT now(),
row_version integer NOT NULL DEFAULT 1,
CONSTRAINT pk_{0} PRIMARY KEY (id)
);", table.Name);
command.ExecuteNonQuery();
}
}
catch (NpgsqlException exception)
{
throw new Exception(String.Format("Could not create table {0}; see the inner exception for more information.", table.Name), exception);
}
finally
{
connection.Dispose();
}
_tables[type] = table;
return table;
}
}
示例14: Connect
private void Connect ()
{
try {
string connectionString;
connectionString = "Server=" + Configuration.DatabaseHost + ";";
connectionString += "Database=builder;User ID=builder;";
if (!string.IsNullOrEmpty (Configuration.DatabasePort))
connectionString += "Port=" + Configuration.DatabasePort + ";";
dbcon = new NpgsqlConnection (connectionString);
Logger.Log (2, "Database connection string: {0}", connectionString);
dbcon.Open ();
object db_now_obj = ExecuteScalar ("SELECT now();");
DateTime db_now;
DateTime machine_now = DateTime.Now;
const string format = "yyyy/MM/dd HH:mm:ss.ffff";
if (db_now_obj is DateTime) {
db_now = (DateTime) db_now_obj;
} else {
Logger.Log ("now () function return value of type: {0}", db_now_obj == null ? "null" : db_now_obj.GetType ().FullName);
db_now = machine_now;
}
db_time_difference = db_now - machine_now;
Logger.Log (2, "DB now: {0}, current machine's now: {1}, adjusted now: {3}, diff: {2} ms", db_now.ToString (format), machine_now.ToString (format), db_time_difference.TotalMilliseconds, Now.ToString (format));
} catch {
if (dbcon != null) {
dbcon.Dispose ();
dbcon = null;
}
throw;
}
}
示例15: GetTransactionEventsUsingQuery
private IEnumerable<TransactionEvent> GetTransactionEventsUsingQuery(NpgsqlCommand command)
{
NpgsqlConnection connection = null;
NpgsqlDataReader reader = null;
try
{
connection = new NpgsqlConnection(_connectionString);
connection.Open();
command.Connection = connection;
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
yield return GetTransactionEventFromRecord(reader);
}
}
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
if (command != null)
{
command.Dispose();
}
if (connection != null)
{
connection.Close();
connection.Dispose();
}
}
}