C# 存取PostgreSQL


资料来源:http://boywhy.blogspot.tw/2014/12/ccpostgresql.htm…

元件下载URL: http://pgfoundry.org/projects/npgsql
http://pgfoundry.org/frs/?group_id=1000140&release…

首先要记得Using Npgsql的东西进来

01.
using Npgsql;//接下来说明连接字串所表示的意义 

02.
NpgsqlConnection conn = new NpgsqlConnection(“Server=127.0.0.1;Port=5432;User Id=user;Password=1234;Database=Demo;”);
conn.Open();

03.
NpgsqlCommand command = new NpgsqlCommand(“select * from shop_sale”, conn);

04.          
try
{
    NpgsqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        string shop = reader[“shop”].ToString(); ;
        string no = reader[“no”].ToString(); ;

        Console.WriteLine(shop + “\t” + no);
    }
}
finally
{
    conn.Close();
}

05.
NpgsqlConnection和NpgsqlCommand 元件必须成双成对


 


相关文章