Guest User

Untitled

a guest
Apr 27th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Npgsql;
  6.  
  7. namespace GooglePing.Database
  8. {
  9.     public class PostgreSaver : IDbSaver
  10.     {
  11.         public PostgreSaver()
  12.         {
  13.             npgsqlConnection = new NpgsqlConnection();
  14.             NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
  15.             builder.Host = "10.10.1.11";
  16.             builder.Port = 5432;
  17.             builder.UserName = "postgres";
  18.             builder.Password = "123";
  19.             npgsqlConnection.ConnectionString = builder.ConnectionString;
  20.             npgsqlConnection.Open();
  21.         }
  22.  
  23.         public void Save(WebLog webLog)
  24.         {
  25.             lock (connectLock)
  26.             {
  27.                 using (NpgsqlCommand command = new NpgsqlCommand())
  28.                 {
  29.                     command.CommandText = "insert into ping(ping_id, name, url, ip) values(nextval('seq_ping'), @name, @url, @ip)";
  30.                     command.Parameters.Add(new NpgsqlParameter("name", webLog.Name));
  31.                     command.Parameters.Add(new NpgsqlParameter("url", webLog.Url));
  32.                     command.Parameters.Add(new NpgsqlParameter("ip", webLog.Ip));
  33.                     command.Connection = npgsqlConnection;
  34.                     command.ExecuteNonQuery();
  35.                 }
  36.             }
  37.         }
  38.  
  39.         private NpgsqlConnection npgsqlConnection;
  40.         private object connectLock = new object();
  41.     }
  42. }
Add Comment
Please, Sign In to add comment