“使用mysql和vb.net,PROCEDURE的参数数量不正确”

时间:2011-12-20 07:35:40

标签: mysql vb.net stored-procedures datagridview

我已经尝试了几天,并尝试让存储过程在vb.net中运行,但我似乎无法使其工作。

如果有人能帮助我,我真的很感激。我一直在努力解决这个问题,但我找不到解决方案。

Private Sub getID()
    Dim ds As New DataSet
    Dim con_str = ""
    Dim con As MySql.Data.MySqlClient.MySqlConnection = New MySql.Data.MySqlClient.MySqlConnection(con_str)
    con.Open()
    Dim cmd As MySql.Data.MySqlClient.MySqlCommand = New MySql.Data.MySqlClient.MySqlCommand
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "getID()"
    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.AddWithValue("@id", "113")
    Dim da As MySql.Data.MySqlClient.MySqlDataAdapter = New MySql.Data.MySqlClient.MySqlDataAdapter(cmd)
    Dim dt As New DataTable
    da.SelectCommand.Connection = con
    da.Fill(dt)
End Sub

当我尝试运行此操作时,我在行da.Fill(dt)上获得异常“PROCEDURE otrs.getID的参数数量不正确;预期为1,得到0”。我真的不知道为什么会这样。我的存储过程中没有声明参数。

我的存储过程应该没问题。从dbms中调用它很好,但是这里仍然是代码:

delimiter $$
CREATE DEFINER=`otrs`@`%` PROCEDURE `getCIs`()
BEGIN
SELECT  
    l.source_key AS base_id,  
    c1.configitem_number AS base_number,
    v.name as base_name,
    l.target_key AS link_id,  
    c2.configitem_number AS link_number,
    v1.name as link_name,
    t.name as link_type
FROM  
    otrs.link_relation AS l
        INNER JOIN  
    otrs.configitem AS c1 
        ON l.source_key = c1.id     
        INNER JOIN  
    otrs.configitem AS c2 
        ON l.target_key = c2.id    
        INNER JOIN
    otrs.link_type AS t
        INNER JOIN
    otrs.configitem_version AS v
        ON l.source_key = v.configitem_id
        INNER JOIN
    otrs.configitem_version AS v1
        ON l.target_key = v1.configitem_id;
END$$

是的,我确实使用了搜索功能,而且我搜索了很多内容,但我仍然感谢任何关于我的问题的信息。

所以我发现错了。显然你不需要在procedure-name之后的()。我不是100%肯定,因为现在根本没有发生任何事情:

Private Sub getID()
    Dim ds As New DataSet
    Dim con_str = ""
    Dim con As MySql.Data.MySqlClient.MySqlConnection = New MySql.Data.MySqlClient.MySqlConnection(con_str)
    con.Open()
    Dim cmd As MySql.Data.MySqlClient.MySqlCommand = New MySql.Data.MySqlClient.MySqlCommand
    cmd.CommandType = CommandType.StoredProcedure
    Try
        cmd.CommandText = "getID"
    Catch ex As Exception
        MsgBox(ex.Message())
    End Try

    cmd.Parameters.AddWithValue("@id", 113)
    cmd.Connection = con
    Dim da As MySql.Data.MySqlClient.MySqlDataAdapter = New MySql.Data.MySqlClient.MySqlDataAdapter(cmd)
    Dim dt As New DataTable
    Try
        da.Fill(dt)
    Catch ex As Exception
        MsgBox(ex.Message())
    End Try
End Sub

2 个答案:

答案 0 :(得分:1)

你永远不会真正执行cmd。另外,如上所述,proc调用不需要括号。看起来应该是这样的。

MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("getID", con);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);

答案 1 :(得分:0)

这行错误,它使用“call getID()”命令文本创建一个新命令,并将其添加到没有参数的适配器

  Dim da As MySql.Data.MySqlClient.MySqlDataAdapter = _
   New MySql.Data.MySqlClient.MySqlDataAdapter("call getID()", con_str)

你想要

  cmd.CommandText = "getID"  
  Dim da As MySql.Data.MySqlClient.MySqlDataAdapter = _
   New MySql.Data.MySqlClient.MySqlDataAdapter(cmd, con_str)