完全分析.NET 2005如何連接PgSQL數(shù)據(jù)庫

字號:


    本文提供了兩種方法幫你分析.NET2005如何連接PgSQL數(shù)據(jù)庫。
    方法一:使用商業(yè)組件:PostgreSQLDirect。
    下載后安裝在.net的安裝目錄下。會在.NET2005工具欄出現(xiàn)PostgreSQLDirect組件包含了PgSqlConnection PgSqlCommand PgSqlDataAdapter 等控件,然后在項目里添加引用:CoreLab.Data和CoreLab.PostgreSql,可以拖放控件連接數(shù)據(jù)庫,使用方法2005的和自帶控件基本相同。也可以寫代碼連接數(shù)據(jù)庫,具體代碼如下:
    PgSqlDataSet ds = new PgSqlDataSet();
    string sql = "select * from onetest where tid=3000";
    PgSqlConnection con = new PgSqlConnectio
    ("user id=postgres;Password=111111;
    host=LOCALHOST;database=postgres");
    PgSqlDataAdapter da = new PgSqlDataAdapter(sql, con);
    da.Fill(ds);
    this.dataGridView1.DataSource = ds;
    this.dataGridView1.DataMember = ds.Tables[0].ToString();
    方法二:下載:Npgsql1.0-bin-ms2.0.zip。
    解壓縮后將其中的兩個dll文件復(fù)制到工程目錄下(和bin同級),然后在項目里添加引:Mono.Security和NPgSQL,在代碼里添加using NpgSQL;具體代碼如下:
    string sql = "select * from onetest where tid=3000";
    NpgsqlConnection con = new NpgsqlConnection
    ("server=localhost;uid=postgres;pwd=111111;database=postgres");
    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql,con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    this.dataGridView1.DataSource = ds;
    this.dataGridView1.DataMember = ds.Tables[0].ToString();