0

I want to connect to a postgresql database and use an insert query. I have done some research but i have no clue what i am doing. So i don't even know if i am connected with the postgresql.

here is my code:

String conn = ("Server=127.0.0.1;Port=5432;User id=*******;Password=***;Database=database1;");
try
{
    NpgsqlConnection objConn = new NpgsqlConnection(conn);
    objConn.Open();
    string strSelectCmd = "INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);";
    NpgsqlDataAdapter objDataAdapter = new NpgsqlDataAdapter(strSelectCmd, objConn);

    objConn.Close();
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.Message, "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
4
  • I have done some research but i have no clue what i am doing - do you have a clue whether you are getting an exception when you run this code? Commented Jul 2, 2013 at 8:08
  • No I dont get any exception. Commented Jul 2, 2013 at 8:11
  • What happens when you run the code? Commented Jul 2, 2013 at 8:17
  • @DarinDimitrov I have used the awnser below and now i get this The type or namespace name 'NpgSqlCommand' could not be found (are you missing a using directive or an assembly reference? Commented Jul 2, 2013 at 8:22

1 Answer 1

1

Refer Following Code:

String conn = ("Server=127.0.0.1;Port=5432;User id=*******;Password=***;Database=database1;");
            try
            {
                NpgsqlConnection objConn = new NpgsqlConnection(conn);
                objConn.Open();
                string strSelectCmd = "INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);";

               NpgSqlCommand cmd=new NpgSqlCommand(strSelectCmd,objConn);
               cmd.ExcuteNonquery();

               objConn.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
     }
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting this error: The type or namespace name 'NpgSqlCommand' could not be found (are you missing a using directive or an assembly reference?)