为了访问PostgreSQL数据库,需要从pgfoundry网站,下载Npgsql .Net Data Provider for Postgresql的组件。
访问 URL:http://pgfoundry.org/frs/?group_id=1000140,请按照自己的VS及.NET版本下载相应的版本。下载后解压缩zip文件,把解压缩的目录中的Npgsql.dll、Mono.Security.dll复制到C#工程的obj目录下,然后在VS中添加引用using Npgsql即可;
可以参照:
1.C#访问MS SQL Server数据库;
2.Python访问MS SQL Server数据库;
3.Java访问MS SQL Server数据库;
(1)按照C#利用SQLConnection访问SQL Server数据库的习惯,首先新建一个DBUSer.cs的类,用来保存用户登录信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;public abstract class DBUser
{public static string ServerPort;public static string UserName;public static string ServerName;public static string DBName;public static string Pwd;public static int LoginFlag;
}
(2)新建一个MyPostDB.cs类,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using Npgsql;public class MyPostDB
{DataSet DS;bool ECode;string ErrString;NpgsqlConnection Conn = new NpgsqlConnection();public MyPostDB(string ServerName,string ServerPort,string DBName,string UserName,string Pwd){ECode = false;Conn.ConnectionString = "Server=" + ServerName + ";Port=" +ServerPort + ";User Id=" + UserName + ";Password=" + Pwd + ";Database=" + DBName;try{Conn.Open();}catch(Exception e){ECode = true;ErrMessage=e.Message;}}public DataSet GetRecordSet(string sql){NpgsqlCommand sqlCmd = new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = sql ;try{NpgsqlDataAdapter adp = new NpgsqlDataAdapter(sqlCmd);DS = new DataSet();adp.Fill(DS);}catch (Exception e){ErrString = e.Message;ECode = true;return null;}return DS;}public int ExecuteSQLScalar(string Sqls){string s;NpgsqlCommand sqlCmd= new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = Sqls;sqlCmd.CommandType = CommandType.Text;try{s = sqlCmd.ExecuteScalar().ToString();}catch (Exception e){ErrString = e.Message;ECode = true;return -1;}return(int.Parse(s)); }public string ExecuteSQLScalarTOstring(string Sqls){string s;NpgsqlCommand sqlCmd = new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = Sqls;sqlCmd.CommandType = CommandType.Text;try{s = sqlCmd.ExecuteScalar().ToString();}catch (Exception e){ErrString = e.Message;ECode = true;return "-1";}return s;}public string ExecuteSQLWithTrans(string Sqls){string s;NpgsqlTransaction myTrans;myTrans=Conn.BeginTransaction();NpgsqlCommand sqlCmd = new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = Sqls;sqlCmd.CommandType = CommandType.Text;sqlCmd.Transaction =myTrans;sqlCmd.ExecuteNonQuery(); //Sqls="SELECT @@IDENTITY AS ID";sqlCmd.CommandText =Sqls;try{s = sqlCmd.ExecuteScalar().ToString();}catch (Exception e){ErrString = e.Message;ECode = true;myTrans.Commit(); return "";}myTrans.Commit();return(s);}public void ExecuteSQL(string Sqls){NpgsqlCommand sqlCmd = new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = Sqls;sqlCmd.CommandType = CommandType.Text;try{sqlCmd.ExecuteNonQuery();}catch (Exception e){ErrString = e.Message;ECode = true;}}public NpgsqlDataReader DBDataReader(string Sqls){NpgsqlCommand sqlCmd = new NpgsqlCommand();sqlCmd.Connection = Conn;sqlCmd.CommandText = Sqls;sqlCmd.CommandType = CommandType.Text;try{return sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);}catch (Exception e){ErrString = e.Message;ECode = true;return null;}}public void DBClose(){try{Conn.Close();}catch (Exception e){ErrString = e.Message;ECode = true;}}public bool ErrorCode(){return ECode;}public string ErrMessage(){return ErrString;}~MyPostDB(){}
}