3

我该如何解决这个问题?我缺少我的代码或功能代码吗?

这是我的 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

4

2 回答 2

8

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

cmd.Parameters.Add("@ireturnvalue", MySqlDbType.Int32);
cmd.Parameters["@ireturnvalue"].Direction = ParameterDirection.ReturnValue;
于 2013-04-01T05:14:33.060 回答
0

您不能使用 ExecuteScalar 函数的返回值 - 您需要返回 returnValue 的参数值:

public async Task<int> ExecuteIntScalarAsync(string functionName, IEnumerable<MySqlParameter> parameters)
    {
    using (var con = new MySqlConnection({connectionString}))
    {
        await con.OpenAsync();
        MySqlCommand cmd = new MySqlCommand(functionName) { CommandType = System.Data.CommandType.StoredProcedure, Connection = con };

        if (parameters != null)
           foreach (var parameter in parameters)
               cmd.Parameters.Add(parameter);
        var returnValue = new MySqlParameter("returnvalue", MySqlDbType.Int32);
        returnValue.Direction = ParameterDirection.ReturnValue;

        await cmd.ExecuteScalarAsync();
        return (int)returnValue.Value;
    }
}
于 2020-01-10T09:59:36.923 回答