这几天想用C#操作postgreSQl数据库,总是出问题,哪位达人有C#连接操作PostgreSQL的代码,能不能看一下!
email: [email protected]

解决方案 »

  1.   

    我觉你的问题关键在于连接,只要连接成功基本上就没什么问题
    至于连接去
    www.connectionstrings.com
    看看。
      

  2.   

    谢谢Knight94(愚翁),不过我下了Npgsql和Core Labs PostgreSQLDirect (.NET) 
    可不会用呀,请详细说说怎么连。
      

  3.   

    参看:
    www.codeproject.com/cs/database/minosse.asp
      

  4.   

    哈 首先你要去下载一个postgresql的odbc驱动程序,然后用odbc连就可以了
    string strCn="User Id=postgres;Password=password;Host=localhost;Database=database;
    然后剩下的和别的数据库操作都一样了
      

  5.   

    我的一段代码
    用的是odbc驱动using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //
    using System.Data.Odbc;
    using System.IO;
    using System.Drawing.Imaging;namespace ImageTest
    {
        public partial class Form1 : Form
        {
            const string connectionString = "Driver={PostgreSQL};Server=192.168.100.100;Database=phpimage;Username=system1;Password=bl0il7;";        public Form1()
            {
                InitializeComponent();
            }        private void btnTest_Click(object sender, EventArgs e)
            {
                List<byte> lb = new List<byte>();            using (OdbcConnection thisConnection = new OdbcConnection(connectionString))
                {
                    thisConnection.Open();
                    string odbcCommand = "Select Count(*) from pg_largeobject where loid=59438";
                    OdbcCommand thisCommand = new OdbcCommand(odbcCommand, thisConnection);
                    int num = Convert.ToInt32(thisCommand.ExecuteScalar());                //string odbcCommand0 = "Select data From pg_largeobject Where loid=59438 and pageno=0";
                    //string odbcCommand1 = "Select data From pg_largeobject Where loid=59438 and pageno=1";
                    //
                    odbcCommand = "Select data From pg_largeobject Where loid=59438 and pageno=";
                    for (int i = 0; i < num; i++)
                    {
                        thisCommand = new OdbcCommand(odbcCommand + i.ToString(), thisConnection);
                        ////using (OdbcDataReader thisReader = thisCommand.ExecuteReader())
                        ////{
                        ////    thisReader.Read();
                        ////    byte[] b = (byte[])thisReader[0];
                        ////    lb.AddRange(b);
                        ////}
                        byte[] b = (byte[])thisCommand.ExecuteScalar();
                        ////////////////////string b = (string)thisCommand.ExecuteScalar();
                        lb.AddRange(b);
                    }
                    byte[] resultBytes = new byte[lb.Count];
                    lb.CopyTo(resultBytes);                FileStream fs = new FileStream("59438-new.jpg", FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(resultBytes);
                    bw.Close();
                    fs.Close();
                    
                    MemoryStream ms = new MemoryStream(resultBytes);
                    //ms.Write(resultBytes, 0, jpgSize);
                    //Image image = new Bitmap(ms);
                    Image image = Bitmap.FromStream(ms);
                    ms.Close();
                    pbDisplay.Image = image;
                                    //pbLender.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    ////这时流的Position应该是ms.Length-1
                    //byte[] b = new byte[ms.Length];
                    //ms.Position = 0;//你没这句,就真接读了,所以你的b中应该是清一色的0x00;
                    //ms.Read(b, 0, (int)ms.Length);
                    //ms.Close();            }            //SqlConnection conn = new SqlConnection();
                //conn.ConnectionString = "Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
                //string strSql = "select FImage from test where id=1";
                //SqlCommand cmd = new SqlCommand(strSql, conn);
                //conn.Open();
                //SqlDataReader reader = cmd.ExecuteReader();
                //reader.Read();
                //MemoryStream ms = new MemoryStream((byte[])reader["FImage"]);            //Image image = Image.FromStream(ms, true);
                //reader.Close();
                //conn.Close();
                //ptbShow.Image = image; 
            }        private void btnTest2_Click(object sender, EventArgs e)
            {
                StringBuilder sbTest=new StringBuilder();
                using (OdbcConnection thisConnection = new OdbcConnection(connectionString))
                {
                    thisConnection.Open();
                    string odbcCommand0 = "Select data From pg_largeobject Where loid=59438 and pageno=0";
                    string odbcCommand1 = "Select data From pg_largeobject Where loid=59438 and pageno=1";
                    OdbcCommand thisCommand = new OdbcCommand(odbcCommand0, thisConnection);
                    byte[] bTest=(byte[])thisCommand.ExecuteScalar();
                    thisCommand = new OdbcCommand(odbcCommand1, thisConnection);
                    string str = (string)thisCommand.ExecuteScalar();
                    sbTest.Append(str);
                    tbTest.Text = sbTest.ToString();
                }
            }
        }
    }