LoginSignup
0
0

More than 3 years have passed since last update.

c# posgre 接続 createdb

Posted at
Program.cs

using System;
using Npgsql;

namespace db02
{
    class Program
    {
        static void Main(string[] args){

        //接続文字列
        NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User ID=postgres;Database=postgres;Password=Chatdb01;Enlist=true");

            conn.Open();
            try {
                NpgsqlCommand command = new NpgsqlCommand("DROP DATABASE IF EXISTS test01", conn);
                command.ExecuteNonQuery();
                command = new NpgsqlCommand("CREATE DATABASE test01", conn);
                command.ExecuteNonQuery();
                string comment = "'Test Database " + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss") + "'";
                command = new NpgsqlCommand("COMMENT ON DATABASE test01 IS " + comment, conn);
                command.ExecuteNonQuery();
            } finally {
                conn.Close();
            }

            conn = new NpgsqlConnection("Server=localhost;Port=5432;User ID=postgres;Database=test01;Password=Chatdb01;Enlist=true");
            conn.Open();
            try {
                NpgsqlCommand command = new NpgsqlCommand("select version()", conn);
                command.ExecuteNonQuery();
                var serverversion = (String)command.ExecuteScalar();
                Console.WriteLine("PostgreSQL server version: {0}", serverversion);
            } finally {
            conn.Close();
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0