使用 Npgsql.dll 链接 PostgreSQL 数据库

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Npgsql;
using NpgsqlTypes;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      using (var conn = new NpgsqlConnection("Host=192.168.1.12;Username=postgres;Password=123456;Database=postgres"))
      {
        conn.Open();
        using (var cmd = new NpgsqlCommand())
        {
          cmd.Connection = conn;
          cmd.CommandText = "SELECT * FROM \"Teacher\";";
          using (var reader = cmd.ExecuteReader())
          {
            while (reader.Read())
            {
              string id = reader["id"].ToString();
              string name = reader["name"].ToString();
              Console.WriteLine(id+"   "+name);
            }
            Console.ReadKey();
            reader.Close();
          }
        }
      }
    }
  }
}

你可能感兴趣的:(数据库)