【问题标题】:cannot insert and save data to a table connected to postgresql on window forms无法在窗口窗体上将数据插入和保存到连接到 postgresql 的表中
【发布时间】:2020-05-17 21:00:34
【问题描述】:

我无法在我的窗体程序中的表格上插入和保存数据。

窗体上的代码是:

private void BtnInsert_Click(object sender, EventArgs e)
        {
            rowIndex = -1;
            textIdentificacao.Enabled = textNome.Enabled = true;
            textIdentificacao.Text = textNome.Text = null;
            textIdentificacao.Select();

        }


private void BtnSave_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (rowIndex < 0)//insert
            {

                try
                {
                    conn.Open();
                    sql = @"Select * from insert(:_docente_id,:_nome_docente)";
                    cmd = new NpgsqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text);
                    cmd.Parameters.AddWithValue("_nome_docente", textNome.Text);                  
                    result = (int)cmd.ExecuteScalar();
                    conn.Close();
                    if (result == 1)
                    {
                        MessageBox.Show("Inserido com sucesso.");
                        Select();
                    }
                    else
                    {
                        MessageBox.Show("Inserção falhou.");
                    }
                }
                catch (Exception ex)
                {
                    conn.Close();
                    MessageBox.Show("Inserção falhou. Error:" + ex.Message);

                }

            }
            result = 0;
            textIdentificacao.Text = textNome.Text = null;
            textIdentificacao.Enabled = textNome.Enabled = false;
        }
    }

posgresql中的函数是:

create or replace function insert(_docente_id int, _nome_docente varchar)
returns int as
$$
begin
    insert into docente(docente_id, nome_docente)
    values(_docente_id, _nome_docente);
    if found then
    return 1;
    else return 0;
    end if;
end
$$
language plpgsql

错误是: 错误 42883 函数 insert(text, text) 不存在

我认为问题与 int 和 text 有关。 因为 postgresql 中的函数有一个 int 和一个 varchar 而在 c# 中我正在使用 cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text); cmd.Parameters.AddWithValue("_nome_docente", textNome.Text);

谢谢

【问题讨论】:

    标签: c# postgresql winforms


    【解决方案1】:

    换线试试

    cmd.Parameters.AddWithValue("_docente_id", textIdentificacao.Text)
    

    cmd.Parameters.Add("_docente_id", SqlDbType.Int);
    cmd.Parameters["_docente_id"].Value = int.Parse(textIdentificacao.Text);
    

    甚至只是cmd.Parameters.AddWithValue("_docente_id", int.Parse(textIdentificacao.Text));

    【讨论】:

    • @RaquelWill0 很高兴为您提供帮助,如果它们对您有用,请不要忘记将答案标记为已接受;)
    • 嗨大师,我还没有足够的声誉,但一旦我拥有它,我会的。我试了几次,但它没有让我标记。
    • @RaquelWill0 有趣,NP =)
    • :) 我可以评价你。谢谢@gurustron
    • @RaquelWill0 你也可以accept answer =) TBH 你不需要为此评分)
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多