Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. trouble in connecting to Postgre SQL from web server.

trouble in connecting to Postgre SQL from web server.

Scheduled Pinned Locked Moved Database
databasecsharpsysadmintestingbeta-testing
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hellono1
    wrote on last edited by
    #1

    Hi all. I'm now developing a website that connect to Postgre SQL database. I used NpgSQL.DLL to connect from C# to database. I also finished testing this project in my local pc, it worked well. But unfortunately it did not work well in server. Some errors in connecting to server happened. These errors happened when website had much people logged in and began using database query functions ( select, update, delete .. ). I think errors begin from codes in connect database functions. but i still can not fix it. this is my connecting database codes. ------ codes connects database ---------

    -- Connect.cs file.
    public static void connectDB()
    {
    try
    {
    conn.ConnectionString = connection;
    conn.Open();
    }
    catch
    {
    //closeConnectDB();
    }
    }
    public static void closeConnectDB()
    {
    //conn.Dispose();
    conn.Close();
    //conn.Dispose();
    }
    public static int ExecuteQuery(string SQL)
    {
    try
    {
    connectDB();
    NpgsqlCommand cmd = new NpgsqlCommand(SQL, conn);
    return cmd.ExecuteNonQuery();
    }
    catch
    {
    closeConnectDB();
    return 0;
    }

    }
    
    public static DataSet SelectQuery(string SQL)
    {
        DataSet ds = new DataSet();
        try
        {
            connectDB();
            NpgsqlDataAdapter nDa = new NpgsqlDataAdapter(SQL, conn);
            
            nDa.Fill(ds);
            // closeConnectDB();
            return ds;
        }
        catch
        {
            closeConnectDB();
            return ds;
        }
        
    }
    

    ----------------- use to execute these codes -------- ....

    --- calling functions files.
    try
    {
    string strTest = "Select * from abc where .. ";
    DataSet ds1 = new DataSet();
    ds1 = Connect.SelectQuery(strTest);
    if (ds1.Tables[0].Rows.Count > 0)
    {

                            }
                            else
                            {
                                int kq = Connect.ExecuteQuery(strInsert);
                                if (kq > 0)
                                {
                                    int
    
    P L 2 Replies Last reply
    0
    • H hellono1

      Hi all. I'm now developing a website that connect to Postgre SQL database. I used NpgSQL.DLL to connect from C# to database. I also finished testing this project in my local pc, it worked well. But unfortunately it did not work well in server. Some errors in connecting to server happened. These errors happened when website had much people logged in and began using database query functions ( select, update, delete .. ). I think errors begin from codes in connect database functions. but i still can not fix it. this is my connecting database codes. ------ codes connects database ---------

      -- Connect.cs file.
      public static void connectDB()
      {
      try
      {
      conn.ConnectionString = connection;
      conn.Open();
      }
      catch
      {
      //closeConnectDB();
      }
      }
      public static void closeConnectDB()
      {
      //conn.Dispose();
      conn.Close();
      //conn.Dispose();
      }
      public static int ExecuteQuery(string SQL)
      {
      try
      {
      connectDB();
      NpgsqlCommand cmd = new NpgsqlCommand(SQL, conn);
      return cmd.ExecuteNonQuery();
      }
      catch
      {
      closeConnectDB();
      return 0;
      }

      }
      
      public static DataSet SelectQuery(string SQL)
      {
          DataSet ds = new DataSet();
          try
          {
              connectDB();
              NpgsqlDataAdapter nDa = new NpgsqlDataAdapter(SQL, conn);
              
              nDa.Fill(ds);
              // closeConnectDB();
              return ds;
          }
          catch
          {
              closeConnectDB();
              return ds;
          }
          
      }
      

      ----------------- use to execute these codes -------- ....

      --- calling functions files.
      try
      {
      string strTest = "Select * from abc where .. ";
      DataSet ds1 = new DataSet();
      ds1 = Connect.SelectQuery(strTest);
      if (ds1.Tables[0].Rows.Count > 0)
      {

                              }
                              else
                              {
                                  int kq = Connect.ExecuteQuery(strInsert);
                                  if (kq > 0)
                                  {
                                      int
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Looking at your code, I can't help but think you're being hammered because you have static methods in a multi user system, and the code isn't thread safe. In other words, you have static variables (take a look at your connection variable), and you are opening and closing them without any form of synchronisation in place. In other words, you could have two users both "open" a connection at the same time, and then one finishes and closes the connection before the other one completes. The easiest fix is to move the open and close logic into the other methods, rather than relying on member variables.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      H 1 Reply Last reply
      0
      • H hellono1

        Hi all. I'm now developing a website that connect to Postgre SQL database. I used NpgSQL.DLL to connect from C# to database. I also finished testing this project in my local pc, it worked well. But unfortunately it did not work well in server. Some errors in connecting to server happened. These errors happened when website had much people logged in and began using database query functions ( select, update, delete .. ). I think errors begin from codes in connect database functions. but i still can not fix it. this is my connecting database codes. ------ codes connects database ---------

        -- Connect.cs file.
        public static void connectDB()
        {
        try
        {
        conn.ConnectionString = connection;
        conn.Open();
        }
        catch
        {
        //closeConnectDB();
        }
        }
        public static void closeConnectDB()
        {
        //conn.Dispose();
        conn.Close();
        //conn.Dispose();
        }
        public static int ExecuteQuery(string SQL)
        {
        try
        {
        connectDB();
        NpgsqlCommand cmd = new NpgsqlCommand(SQL, conn);
        return cmd.ExecuteNonQuery();
        }
        catch
        {
        closeConnectDB();
        return 0;
        }

        }
        
        public static DataSet SelectQuery(string SQL)
        {
            DataSet ds = new DataSet();
            try
            {
                connectDB();
                NpgsqlDataAdapter nDa = new NpgsqlDataAdapter(SQL, conn);
                
                nDa.Fill(ds);
                // closeConnectDB();
                return ds;
            }
            catch
            {
                closeConnectDB();
                return ds;
            }
            
        }
        

        ----------------- use to execute these codes -------- ....

        --- calling functions files.
        try
        {
        string strTest = "Select * from abc where .. ";
        DataSet ds1 = new DataSet();
        ds1 = Connect.SelectQuery(strTest);
        if (ds1.Tables[0].Rows.Count > 0)
        {

                                }
                                else
                                {
                                    int kq = Connect.ExecuteQuery(strInsert);
                                    if (kq > 0)
                                    {
                                        int
        
        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        stop your static abuse, and start using the using construct[^], it fits quite well in DB applications. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        H 1 Reply Last reply
        0
        • P Pete OHanlon

          Looking at your code, I can't help but think you're being hammered because you have static methods in a multi user system, and the code isn't thread safe. In other words, you have static variables (take a look at your connection variable), and you are opening and closing them without any form of synchronisation in place. In other words, you could have two users both "open" a connection at the same time, and then one finishes and closes the connection before the other one completes. The easiest fix is to move the open and close logic into the other methods, rather than relying on member variables.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          H Offline
          H Offline
          hellono1
          wrote on last edited by
          #4

          thanks. i did it and my problems solved.

          1 Reply Last reply
          0
          • L Luc Pattyn

            stop your static abuse, and start using the using construct[^], it fits quite well in DB applications. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            H Offline
            H Offline
            hellono1
            wrote on last edited by
            #5

            thank u so much.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups