c# postgresql 数据库

最近开始做C#DotNet的工作,因为对PostgreSQL数据库比较有兴趣,所以自己研究了一下如何访问PostgreSQL

数据库的问题。

1、为了访问PostgreSQL数据库,需要从pgfoundry网站,下载Npgsql .Net Data Provider for Postgresql的组件。

访问 URLhttp://pgfoundry.org/frs/?group_id=1000140

注:因为使用的是DotNet3.5,所以下载了Npgsql2.0.6-bin-ms.net3.5sp1.zip

2、解压缩zip文件,把解压缩的目录中的Npgsql.dllMono.Security.dll复制到C#工程的obj目录下,然后在VS2008

,把 Npgsql.dll文件加入到References

3、在需要使用NpgsqlC#文件头,加入如下的using语句。

 

using Npgsql;  

using Npgsql;

以上转自:http://blog.csdn.net/FlashDragon/archive/2009/11/09/4789980.aspx

 

4、得到数据库连接

public NpgsqlConnection getConn()

        {

           

            string connectionString = "Server=74.119.206.125;Port=5432;User Id=user;"

+ "Password=identify; Database=databaseName;" + "CommandTimeout=0;ConnectionLifeTime=0;";

            NpgsqlConnection conn = new NpgsqlConnection(connectionString);

            //conn.Open(); Console.WriteLine("successfully!");

            //conn.Close();

            return conn;        

 

        }

5//NpgsqlCommand.ExecuteScalar()方法获得唯一值的检索结果

        public int queryOnly(string  queryString,NpgsqlConnection conn)

        {           

            try

            {               

                conn.Open();

                NpgsqlCommand objCommand = new NpgsqlCommand(queryString, conn);

                int count = Convert.ToInt32(objCommand.ExecuteScalar());

                return count;

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

                return 0;

            }           

        }

6//NpgsqlCommand.ExecuteReader() 方法获得一个结果集的检索结果(connOpenClose调用省略)

        public NpgsqlDataReader queryReader(string queryString,NpgsqlConnection conn)

        {            

            try

            {

                conn.Open();

                NpgsqlCommand objCommand = new NpgsqlCommand(queryString,conn);

                NpgsqlDataReader dr = objCommand.ExecuteReader();

                Console.WriteLine("successfully Reader!");              

                return dr;

              

            }

catch (Exception e)

            {

                Console.WriteLine(e.Message);

                return null;

            } 

 

        }

7//NpgsqlCommand.ExecuteNonQuery()方法对指定表进行添加、更新和删除一条记录的操作

        public void changeTable(string changeString,NpgsqlConnection conn)

        {           

            try

            {

                conn.Open();

                NpgsqlCommand objCommand = new NpgsqlCommand(changeString, conn);

                objCommand.ExecuteNonQuery();

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }           

        }

8//NpgsqlDataAdapter.Fill方法,把检索的结果集放到DataSet object

        public void datasetFill(string fillString,NpgsqlConnection conn)

        {  

            try

            {

                conn.Open();

                DataSet ds = new DataSet();

                NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter(fillString, conn);

                objAdapter.Fill(ds, "a");//a这个表是自定义的

                // dgvBaseResult.DataSource=ds.Tables["a"];//dgvBaseResultdatagridview的一个对象

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }           

        }

 

9//获取表中字段名称&字段类型

        public void dataField(string operationDB,NpgsqlConnection conn)

        {

            NpgsqlDataReader myReader = queryReader(operationDB,conn);

            DataTable myTable = myReader.GetSchemaTable();

           

            //获取字段类型

            for (int i = 0; i < myReader.FieldCount;i++ )

            {

                DataRow row=myTable.Rows[i];

                Console.Write(row[0].ToString()+"\t\t\t");//获取字段名称

                Console.WriteLine(myReader.GetFieldType(i).ToString());//获取字段类型

            }

        }

10、获取postgreSQL 里面某个数据库中所有用户定义的数据表的名字

 

"select * from pg_tables"  —— 得到当前db中所有表的信息(这里pg_tables是系统视图)

 

"select tablename from pg_tables where schemaname='public'"  ——  得到所有用户自定义表的名字(这里"tablename"字段是表的名字,"schemaname"schema的名字。用户自定义的表,如果未经特殊处理,默认都是放在名为publicschema下)

 

更多内容请参考http://npgsql.projects.postgresql.org/docs/manual/UserManual.html

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