C 訪問postgresql資料庫

2021-05-23 14:42:02 字數 1303 閱讀 1153

using npgsql;  

using npgsql; 

4、建立乙個postgresql的資料庫連線 view plaincopy to clipboardprint?

/* 用指定的connectionstring,去例項化乙個npsqlconnection的物件*/ 

string connectionstring = "server=127.0.0.1;port=5432;user id=test;password=test;database=testdb;" 

npgsqlconnection conn = new npgsqlconnection(string connectionstring);  

// 開啟乙個資料庫連線,在執行相關sql之前呼叫  

conn.open();  

//關閉乙個資料庫連線,在執行完相關sql之後呼叫  

conn.close();  

/* 用指定的connectionstring,去例項化乙個npsqlconnection的物件*/

string connectionstring = "server=127.0.0.1;port=5432;user id=test;password=test;database=testdb;"

npgsqlconnection conn = new npgsqlconnection(string connectionstring);

// 開啟乙個資料庫連線,在執行相關sql之前呼叫

conn.open();

//關閉乙個資料庫連線,在執行完相關sql之後呼叫

conn.close();

5、用npgsqlcommand.executescalar()方法獲得唯一值的檢索結果 

try 

finally 

tryfinally

6、用npgsqlcommand.executereader() 方法獲得乙個結果集的檢索結果(conn的open和close呼叫省略)

string sql = "select * from test";  

npgsqlcommand objcommand = new npgsqlcommand(sql,conn);  

npgsqldatareader dr = command.executereader();  

while(dr.read())  

/t", dr[i]); //獲得欄位名  

}  

int testid = dr["id"]; // 獲得指定欄位的值。(id是test表的乙個字段)  

配置PostgreSQL遠端訪問

編輯資料資料夾下的pg hba.conf檔案,找到 type database user address method ipv4 local connections host all all 127.0.0.1 32 md5 host testdb postgres 192.1.1.2 32md5 ...

PostgreSQL 配置遠端訪問

配置遠 程連線postgresql資料庫的步驟很簡單,只需要修改data目錄下的 pg hba.conf 和postgresql.conf。pg hba.conf 配置對資料庫的訪問許可權,postgresql.conf 配置postgresql資料庫伺服器的相應的引數。修改pg hba.conf檔...

PostgreSQL 配置遠端訪問

配置遠 程連線postgresql資料庫的步驟很簡單,只需要修改data目錄下的 pg hba.conf 和postgresql.conf。pg hba.conf 配置對資料庫的訪問許可權,postgresql.conf 配置postgresql資料庫伺服器的相應的引數。修改pg hba.conf檔...