0

我想连接到 postgresql 数据库并使用插入查询。我做了一些研究,但我不知道我在做什么。所以我什至不知道我是否与 postgresql 连接。

这是我的代码:

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

1 回答 1

1

参考以下代码:

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);
            }
     }
于 2013-07-02T08:11:38.427 回答