C# 连接postgresql并使用查询
我想连接到postgresql数据库并使用插入查询。 我做了一些研究,但我不知道我在做什么。 所以我甚至不知道我是否和postgresql有联系 这是我的密码:C# 连接postgresql并使用查询,c#,postgresql,C#,Postgresql,我想连接到postgresql数据库并使用插入查询。 我做了一些研究,但我不知道我在做什么。 所以我甚至不知道我是否和postgresql有联系 这是我的密码: String conn = ("Server=127.0.0.1;Port=5432;User id=*******;Password=***;Database=database1;"); try { NpgsqlConnection objConn = new NpgsqlConnection(conn); objCo
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);
}
参考以下代码:
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);
}
}
我做了一些研究,但我不知道我在做什么
-你知道运行此代码时是否出现异常吗?不,我没有发现任何异常。运行代码时会发生什么情况?@DarinDimitrov我使用了下面的awnser,现在我得到了此消息。找不到类型或命名空间名称“NpgSqlCommand”(您是否缺少using指令或程序集引用?我收到此错误:找不到类型或命名空间名称“NpgSqlCommand”(您是否缺少using指令或程序集引用?)