Skip to content

Instantly share code, notes, and snippets.

@jayu108
Last active March 17, 2016 08:18
Show Gist options
  • Save jayu108/39494f51eec66e6948ba to your computer and use it in GitHub Desktop.
Save jayu108/39494f51eec66e6948ba to your computer and use it in GitHub Desktop.
blog ; c# -- Npgsql 이용하여 postgresql 접속한후 데이터 가져오기
using System;
using Npgsql;
namespace npgsql_test
{
class Program
{
static void Main(string[] args)
{
using (var conn = new NpgsqlConnection("Host=localhost;Username=postgres;Password=1234;Database=test2db"))
{
try
{
conn.Open();
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select * from addresses";
using (var reader = cmd.ExecuteReader())
{
Console.WriteLine("table column 수 = {0} 개", reader.FieldCount);
while (reader.Read())
{
// 각각의 항목 읽어 들이는 방법들...
// Console.WriteLine(reader.GetString(0));
// Console.WriteLine(reader.GetValue(3));
// Console.WriteLine(reader["email_address"] as string);
var data = new string[] { reader["id"].ToString(),
reader["email_address"].ToString(),
reader["user_id"].ToString(),
reader["createtime"].ToString() };
foreach (var x in data)
{
Console.Write(x);
Console.Write(" -- ");
}
Console.WriteLine();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("============== Error ==============");
Console.WriteLine(ex.Message);
}
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment