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 に答える