您的sql语法有错误,mysql connector for.net

mkshixfv  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(318)

储存程序:

CREATE DEFINER=`newdb`@`%` PROCEDURE `GetMaximumIDsCount`(IN AppID INT,IN isBurnCenter INT,IN BurnCenterID INT)
BEGIN

    IF isBurnCenter = 1 THEN

    SELECT MAX(CAST(b.hospitalid AS SIGNED))

    FROM tblhosipital b

    WHERE b.appId = AppID AND b.burncenterid IS NULL;

    END IF;

    IF isBurnCenter = 0 THEN

    SELECT MAX(CAST(h.hospitalid AS SIGNED))

    FROM tblhosipital h

    WHERE h.appId = AppID AND h.burncenterid <> '' AND h.burncenterid = BurnCenterID;

    END IF;
END

从applicationdbcontext调用(代码优先)

public virtual ObjectResult<long?> GetMaximumIDsCount2(Nullable<int> appID, Nullable<int> isBurnCenter, Nullable<int> burnCenterID)
        {
            var appIDParameter = appID.HasValue ?
                new ObjectParameter("AppID", appID) :
                new ObjectParameter("AppID", typeof(int));

            var isBurnCenterParameter = isBurnCenter.HasValue ?
                new ObjectParameter("isBurnCenter", isBurnCenter) :
                new ObjectParameter("isBurnCenter", typeof(int));

            var burnCenterIDParameter = burnCenterID.HasValue ?
                new ObjectParameter("BurnCenterID", burnCenterID) :
                new ObjectParameter("BurnCenterID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<Nullable<long>>("GetMaximumIDsCount @AppID, @isBurnCenter, @BurnCenterID", appIDParameter, isBurnCenterParameter, burnCenterIDParameter);
        }

获取异常:
sql语法有错误;请查看与您的mysql服务器版本对应的手册,以了解在第1行的“getmaximumidscount@appid,@isburnchenter,@burnchenterid”附近使用的正确语法

mtb9vblg

mtb9vblg1#

你需要加上 CALL [procedure_name]() 存储过程名称之前的方法:

return ((IObjectContextAdapter)this)
       .ObjectContext
       .ExecuteStoreQuery<List<long?>>("CALL GetMaximumIDsCount(@AppID, @isBurnCenter, @BurnCenterID)", 
       appIDParameter, isBurnCenterParameter, burnCenterIDParameter);

注意:mysql存储过程使用以下语法执行:

CALL procedure_name(parameter_list)

注2:使用 FirstOrDefault() 或者 SingleOrDefault() 从中只返回一个结果集 ObjectResult<T> :

var x = db.GetMaximumIDsCount2(...).FirstOrDefault();

作为补充说明,您可以尝试用声明参数 MySqlParameter 而不是 ObjectParameter :

// you can change DBNull.Value with any appropriate value

var appIDParameter = new MySqlParameter("AppID", MySqlDbType.Int).Value = (appID.HasValue) ? appID : DBNull.Value;

var isBurnCenterParameter = new MySqlParameter("isBurnCenter", MySqlDbType.Int).Value = (isBurnCenter.HasValue) ? isBurnCenter : DBNull.Value;

var isBurnCenterParameter = new MySqlParameter("BurnCenterID", MySqlDbType.Int).Value = (burnCenterID.HasValue) ? burnCenterID : DBNull.Value;

参考文献:
调用语法(mysql)

相关问题