postgresql 使用 Npgsql postgres 和 C# 更新命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24287396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:30:10  来源:igfitidea点击:

Update command using Npgsql postgres and C#

c#postgresql

提问by Evanark

I have created a search form to locate a record based on a search value called license ID. I am able to pull the all the fields for that value. My question is I am trying to update any field for that record, example if I want to update the address field or the zip code field which all these field values are in text boxes. I get an syntax error at or near ""Lname"". Lname is the database field name for last name. This project is just a test project to get familiar with Postgres. Here is my source code. I am not sure what the problems is, the field name is correctly specified.

我创建了一个搜索表单,用于根据称为许可证 ID 的搜索值来定位记录。我能够提取该值的所有字段。我的问题是我正在尝试更新该记录的任何字段,例如,如果我想更新所有这些字段值都在文本框中的地址字段或邮政编码字段。我在“Lname”处或附近收到语法错误。Lname 是姓氏的数据库字段名称。这个项目只是一个熟悉 Postgres 的测试项目。这是我的源代码。我不确定问题是什么,正确指定了字段名称。

 private void UpdateRecord()
    {
        try
        {
            NpgsqlConnection conn = Connection.getConnection();
            conn.Open();

            NpgsqlCommand cmd = new NpgsqlCommand("update info set \"Fname\" = :FirstName, set \"Lname\" = :LastName, set \"Address\" = :Address," +
                "set \"City\" = :City, set \"State\" = State, set \"Zip\" = :Zip," + 
                "set \"PhoneNumber\" = :PhoneNumber where \"LicenceNumber\" = '" + LicenseID + "' ;", conn);

            cmd.Parameters.Add(new NpgsqlParameter("FirstName", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("LastName", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("Address", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("City", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("State", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("Zip", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters.Add(new NpgsqlParameter("PhoneNumber", NpgsqlTypes.NpgsqlDbType.Text));
            cmd.Parameters[0].Value = txtFirstName.Text;
            cmd.Parameters[1].Value = txtLastName.Text;
            cmd.Parameters[2].Value = txtAddress.Text;
            cmd.Parameters[3].Value = txtCity.Text;
            cmd.Parameters[4].Value = cboState.Text;
            cmd.Parameters[5].Value = txtZip.Text;
            cmd.Parameters[6].Value = mtxtPhoneNumber.Text;

            cmd.ExecuteNonQuery();
            conn.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

回答by iamkrillin

You only need "set" once per command.

每个命令只需要“设置”一次。

NpgsqlCommand cmd = new NpgsqlCommand("update info set \"Fname\" = :FirstName, \"Lname\" = :LastName, \"Address\" = :Address," +
                "\"City\" = :City, \"State\" = State, \"Zip\" = :Zip," + 
                "\"PhoneNumber\" = :PhoneNumber where \"LicenceNumber\" = '" + LicenseID + "' ;", conn);