我该如何解决这个问题?我的代码或功能代码丢失了吗?

这是我的 C# 代码

public int AddDetails(string business, string clerkid, string serverid, string ticket, string tablenumber,
        string sourcecard, string recipientcard, DateTime datesaved, string status)
    {
        CreateConnection();
        int dbId = 0;
        using(cmd = new MySqlCommand())
        {
            cmd.Connection = cn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "AddDetails";
            cmd.Parameters.AddWithValue("m_business", business);
            cmd.Parameters.AddWithValue("m_clerkid", clerkid);
            cmd.Parameters.AddWithValue("m_serverid", serverid);
            cmd.Parameters.AddWithValue("m_ticket", ticket);
            cmd.Parameters.AddWithValue("m_tablenumber", tablenumber);
            cmd.Parameters.AddWithValue("m_sourcecard", sourcecard);
            cmd.Parameters.AddWithValue("m_recipientcard", recipientcard);
            cmd.Parameters.AddWithValue("m_datesaved", datesaved);
            cmd.Parameters.AddWithValue("m_status", status);
            cn.Open();
            //dbId = Convert.ToInt32(cmd.LastInsertedId);
            dbId = int.Parse(cmd.ExecuteScalar().ToString());
            //dbId = Convert.ToInt32(cmd.ExecuteNonQuery());
            cn.Close();
        }
        return dbId;
    }

这是我存储的功能
 CREATE DEFINER=`root`@`192.168.21.%` FUNCTION `AddDetails`(
m_business VARCHAR(1),
m_clerkid VARCHAR(45),
m_serverid VARCHAR(45),
m_ticket VARCHAR(45),
m_tablenumber VARCHAR(45),
m_sourcecard VARCHAR(45),
m_recipientcard VARCHAR(45),
m_datesaved DATETIME,
m_status VARCHAR(45)
) RETURNS int(11)
    READS SQL DATA
BEGIN
    INSERT INTO `tbl_merchantwarehouse_transaction`
(
`business`,
`clerkid`,
`serverid`,
`ticket`,
`tablenumber`,
`sourcecardnumber`,
`recipientcardnumber`,
`datesaved`,
`status`
)
VALUES(
m_business,
m_clerkid,
m_serverid,
m_ticket,
m_tablenumber,
m_sourcecard,
m_recipientcard,
m_datesaved,
m_status
);

RETURN LAST_INSERT_ID();
END

对我的代码的任何建议、建议/改进将不胜感激。谢谢:D

最佳答案

您应该使用如下命令指定返回参数:

cmd.Parameters.Add("@ireturnvalue", MySqlDbType.Int32);
cmd.Parameters["@ireturnvalue"].Direction = ParameterDirection.ReturnValue;

关于c# - 尝试调用存储函数 "dbname"."functionname"不指定返回参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15737935/

10-11 05:13