PostgreSQL安装部署及.net客户端调用

【PostgreSQL】发布了全新的NoSQL特性,引入了新的类型JSONB,已经升级为一款混合型(SQL和NOSQL)数据库。
1.下载安装
https://www.postgresql.org/download/

下载postgresql-9.4.9-1-windows-x64.exe 直接安装,可以调整data的存放路径等。

端口:5432
密码:1-6( 测试密码)

安装完毕:




2.打开pgAdmin,使用pgAdmin来管理pgsql

创建数据库,创建数据表(可选择字段类型为json类型)


使用参考:http://blog.csdn.net/frank_good/article/details/46827601

4..net客户端操作

a.nuget下载依赖库


b..net客户端代码

pgsql管理类:

<span style="font-size:18px;">    class PostgreSqlHandle
    {
        public void InsertRow(string pgConnectionString)
        {
            // If the connection string is empty, use default. 
            if (pgConnectionString == "")
            {
                pgConnectionString =
                    "host=127.0.0.1;database=testDB;user id=postgres;;password=123456;";
            }
            PgSqlConnection myConn = new PgSqlConnection(pgConnectionString);
            string myInsertQuery = @"INSERT INTO ""testOldDB"" (col1, col2) VALUES (550, 355);";
            PgSqlCommand pgCommand = new PgSqlCommand(myInsertQuery);
            pgCommand.Connection = myConn;
            myConn.Open();
            try
            {
                pgCommand.ExecuteNonQuery();
            }
            finally
            {
                myConn.Close();
            }
        }
        public void ReadMyData(string myConnString)
        {
            string mySelectQuery = @"SELECT col1, col2 FROM ""testOldDB"" ";
            PgSqlConnection pgConnection = new PgSqlConnection(myConnString);
            PgSqlCommand pgCommand = new PgSqlCommand(mySelectQuery, pgConnection);
            pgConnection.Open();
            PgSqlDataReader pgReader = pgCommand.ExecuteReader();
            try
            {
                while (pgReader.Read())
                {
                    Console.WriteLine(pgReader.GetInt32(0).ToString() + ", " + pgReader.GetString(1));
                }
            }
            finally
            {
                // always call Close when done reading. 
                pgReader.Close();
                // always call Close when done reading. 
                pgConnection.Close();
            }
        } 
    }</span>

测试:

<span style="font-size:18px;">        private string conStr = "host=127.0.0.1;database=testDB;user id=postgres;;password=123456;";
        private void button1_Click(object sender, EventArgs e)
        {
            PostgreSqlHandle postgreSqlHandle = new PostgreSqlHandle();
            postgreSqlHandle.InsertRow(conStr);//插入行
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PostgreSqlHandle postgreSqlHandle = new PostgreSqlHandle();
            postgreSqlHandle.ReadMyData(conStr);//查询
        }</span>

pgAdmin图形工具查看结果:



展开阅读全文

没有更多推荐了,返回首页