简体   繁体   中英

How do you pass an output parameter from vb.net to a mysql stored procedure?

I am using the .net connector with mysql 5.5. When I try to call a stored procedure with an "out" parameter, I get the message OUT or INOUT argument 2 for routine name is not a variable or NEW pseudo-variable in BEFORE trigger at cmd.ExecuteNonQuery() .

What is wrong?

vb code:

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 stored procedure:

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

Please try this

Create Stored Procedure in MySQl like

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 ;

And Your vb.net code like

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

Let me know if you have any problem...

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM