c#和mysql过程

b91juud3  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(349)

我有两张table。主要项目和帮助项目。
主项目有以下列
(主\u项\u id、主\u项\u名称)
帮助项包含以下列
(帮助\u项目\u id、帮助\u项目\u名称、主要\u项目\u id)。
我写了这个程序

CREATE DEFINER=`root`@`localhost` PROCEDURE `thamer1`(in main_items_id_ int, 
out res int)
BEGIN
declare a int;
declare b int;

select count(help_items_id)
into a from help_items
where main_items_id=main_items_id_;

if a=0 then
    set b=(main_items_id_*10)+1;
    set res=b;
else
    select COALESCE(max(help_items_id),0)+1
    into res
    from help_items
    where main_items_id=main_items_id_;
end if;
END

此过程适用于mysql wrokbench。
这是c代码

private void a_KeyDown(object sender, KeyEventArgs e)
    {
        using (MySqlConnection mysqlcon6 = new 
MySqlConnection(connectString))
        {
            mysqlcon6.Open();
            MySqlCommand mysqlcmd6 = new MySqlCommand("thamer1", mysqlcon6);

            mysqlcmd6.CommandType = CommandType.StoredProcedure;
            mysqlcmd6.CommandText = "thamer1";
            mysqlcmd6.Parameters.Add("@main_items_id_", MySqlDbType.Int32).Value = a.Text;
            mysqlcmd6.Parameters.Add("@res", MySqlDbType.Int32).Value=HITEM.Text;
            mysqlcmd6.ExecuteNonQuery();
           // MessageBox.Show("saved");
            // GridFill();
        }
    }

我从datagrideview中选择value(对于main\u items\u id),并将其放入名为 a .
当我按回车键时,我收到这个消息
system.formatexception:'输入字符串的格式不正确'
我希望能帮助我解决这个错误。

scyqe7ek

scyqe7ek1#

删除此行中设置参数值的部分:

mysqlcmd6.Parameters.Add("@res", MySqlDbType.Int32).Value=HITEM.Text;

看起来你希望这能约束 @resHITEM 文本框,不是这样的。 HITEM.Text 只是一个字符串,当您将该值赋给int参数时,您告诉mysql您希望它能够将该字符串解析为int。
相反,只创建参数,如下所示:

mysqlcmd6.Parameters.Add("@res", MySqlDbType.Int32);

您还需要告诉ado.net这是一个输出参数。然后在查询运行后,通过将参数值指定给hitem.text而不是从hitem.text检查参数值:

private void a_KeyDown(object sender, KeyEventArgs e)
{
    //You can re-use the *names* of these variables, since their scopes are limited to the method
    //You can also stack them to share the same scope block and reduce nesting/indentation
    using (var con = new MySqlConnection(connectString))
    using (var cmd = new MySqlCommand("thamer1", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        //  mysqlcmd6.CommandText = "thamer1"; //you already did this in constructor. Don't need to do it again
        cmd.Parameters.Add("@main_items_id_", MySqlDbType.Int32).Value = a.Text;
        //DON'T assign to the Value, but DO make sure ADO.Net understands this is an OUTPUT parameter
        cmd.Parameters.Add("@res", MySqlDbType.Int32).Direction = ParameterDirection.Output;

        //wait as long as possible to call Open()
        con.Open();
        cmd.ExecuteNonQuery();

        //Now you can assign**to**HITEM.Text, rather than from it.
        HITEM.Text = cmd.Parameters["@res"].Value;
    }
    //End the scope as soon as possible, so the connection can be disposed faster
    // MessageBox.Show("saved");
    // GridFill();
}

这里又是没有任何额外的评论:

private void a_KeyDown(object sender, KeyEventArgs e)
{
    using (var con = new MySqlConnection(connectString))
    using (var cmd = new MySqlCommand("thamer1", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@main_items_id_", MySqlDbType.Int32).Value = a.Text;
        cmd.Parameters.Add("@res", MySqlDbType.Int32).Direction = ParameterDirection.Output;

        con.Open();
        cmd.ExecuteNonQuery();
        HITEM.Text = cmd.Parameters["@res"].Value;
    }
}

更好的做法是将所有sql方法从事件处理程序移到一个单独的类中。事件处理程序应该只需要调用新类中的方法,如下所示:

public static class DB
{
    private static string connectionString = "...";

    public static int thamer(int main_item_id)
    {
        using (var con = new MySqlConnection(connectString))
        using (var cmd = new MySqlCommand("thamer1", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@main_items_id_", MySqlDbType.Int32).Value = main_item_id;
            cmd.Parameters.Add("@res", MySqlDbType.Int32).Direction = ParameterDirection.Output;

            con.Open();
            cmd.ExecuteNonQuery();
            return (int)cmd.Parameters["@res"].Value;
        }
    }
}

private void a_KeyDown(object sender, KeyEventArgs e)
{
    HITEM.Text = DB.thamer(int.Parse(a.Text)).ToString();
}
xcitsw88

xcitsw882#

改变这个

mysqlcmd6.Parameters.Add("@main_items_id_", 
MySqlDbType.Int32).Value = a.Text;
            mysqlcmd6.Parameters.Add("@res", MySqlDbType.Int32).Value = 
HITEM.Text;

int value1 = 0;
int value2 = 0;

if (!Int32.Text.TryParse(a.Text) || !Int32.TryParse(HITEM.Text))
{
    return;
}
 mysqlcmd6.Parameters.Add("@main_items_id_",  MySqlDbType.Int32).Value =  value1;
 mysqlcmd6.Parameters.Add("@res", MySqlDbType.Int32).Value = value2;

相关问题