CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

数据库 | Oracle | DB2 | SQL Server | MySQL | 商业智能 | BI | DBA | Sybase| SQL Server 2008

您现在的位置: IT专家网 > 数据库子站 > 数据库技巧

.NET2005如何连接PgSQL数据库

作者: ,  出处:IT专家网社区, 责任编辑: 叶江, 
2007-05-21 14:27
  本文提供了两种方法帮你分析.NET2005如何连接PgSQL数据库……

  方法一:

  使用商业组件:PostgreSQLDirect。

  下载后安装在.net的安装目录下。会在.NET2005工具栏出现PostgreSQLDirect组件包含了PgSqlConnection PgSqlCommand PgSqlDataAdapter 等控件,然后在项目里添加引用:CoreLab.Data和CoreLab.PostgreSql,可以拖放控件连接数据库,使用方法2005的和自带控件基本相同。也可以写代码连接数据库,具体代码如下:

  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文件复制到工程目录下(和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();

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅


    
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i