如何将输出参数从vb.net传递给mysql存储过程?

时间:2012-10-23 03:54:59

标签: .net mysql vb.net

我正在使用带有mysql 5.5的.net连接器。当我尝试使用“out”参数调用存储过程时,我会在OUT or INOUT argument 2 for routine收到消息is not a variable or NEW pseudo-variable in BEFORE trigger name cmd.ExecuteNonQuery()

有什么问题?

vb代码:

cmd = New MySqlCommand("call testme(@id, @count)", conn)
cmd.Parameters.AddWithValue("@id", id) ' "id" and "count" are integer variables
cmd.Parameters.AddWithValue("@count", count)
cmd.Parameters("@count").Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()

mysql存储过程:

CREATE PROCEDURE testme(in taxid integer, out imageDescCount integer)
BEGIN
set imageDescCount = 23;
End

1 个答案:

答案 0 :(得分:3)

请试试这个

在MySQl中创建存储过程,如

DELIMITER $$
DROP PROCEDURE IF EXISTS `tempdb`.`GetCity` $$
CREATE PROCEDURE  `tempdb`.`GetCity` 
(IN cid INT,
 OUT cname VarChar(50)
)

BEGIN

  SET cname = (SELECT CityName FROM `City` WHERE CID = cid);

END $$

DELIMITER ;

和你的vb.net代码一样

Dim conn As  New MySqlConnection()
conn.ConnectionString = "server=localhost;user=root;database=tempdb;port=3306;password=******;"
Dim cmd As New MySqlCommand()

conn.Open()
cmd.Connection = conn
cmd.CommandText = "GetCity"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@cid", "1")
cmd.Parameters("@cid").Direction = ParameterDirection.Input

cmd.Parameters.AddWithValue("@cname", MySqlDbType.String)
cmd.Parameters("@cname").Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()

Console.WriteLine("City Name: " & cmd.Parameters("@cname").Value) //Access Your Output Value

如果您有任何问题,请告诉我......

由于