首页 > 代码库 > Entity Framework访问MySQL数据库的存储过程并获取返回值

Entity Framework访问MySQL数据库的存储过程并获取返回值

一.创建MySql存储过程

1,

CREATE PROCEDURE `InsertAlarmInfo`(in businessindex int, in providerindex int, in alarmtype int,
            in alarmlevel int,in  detecttime DateTime, in alarmdescription varchar(50), in alarmphenomenon int, in subBusinessType int,
            in businessEntityIndex varchar(50), in taskIndex varchar(50), in eventIndex varchar(50))
BEGIN
 declare entityIndex varchar(50);
 if detecttime is  null then set detecttime=now();  END IF;
 INSERT INTO M_ALARM (BUSINESSINDEX, PROVIDERINDEX, ALARMTYPE, ALARMLEVEL, DETECTTIME, ALARMDESCRIPTION, HANDLESTATUS, ALARMPHENOMENON, ENTITYTYPE) 
 VALUES (businessindex,providerindex, alarmtype, alarmlevel, detecttime, alarmdescription, 3, alarmphenomenon, subBusinessType);
 set entityIndex =concat(‘A_CN‘,right(concat(‘00000000‘,CURRVAL(‘ENTITY_SEQ‘)),8));
  SET SQL_SAFE_UPDATES = 0;
if businessEntityIndex is not null then
INSERT INTO M_CONFIGITEMRELATION (SOURCEINDEX, TARGETINDEX, RELATIONTYPE, GROUPTYPE)
 VALUES (entityIndex,businessEntityIndex, 3, 1);
                  
END IF;                
 if taskIndex is not null then
    INSERT INTO M_CONFIGITEMRELATION (SOURCEINDEX, TARGETINDEX, RELATIONTYPE, GROUPTYPE)
                              VALUES (entityIndex, taskIndex, 3, 5);
END IF;
 if eventIndex is not null then
    INSERT INTO M_CONFIGITEMRELATION (SOURCEINDEX, TARGETINDEX, RELATIONTYPE, GROUPTYPE)
                              VALUES (entityIndex,eventIndex, 3, 14);
    UPDATE M_EVENT SET DEALTYPE = 1 WHERE EVENTINDEX = eventIndex ;
                  
 END IF;
UPDATE M_BUSINESSENTITYEXTEND SET CURRENTSTATUS = 2 WHERE ENTITYINDEX = businessEntityIndex  and ENTITYTYPE = subBusinessType;
SET SQL_SAFE_UPDATES = 1;
select entityIndex;//这里用于返回EF获取的数据
END

2,1(这个存储过程调用函数)

CREATE  PROCEDURE `InsertWorkOrderInfo`(in personindex INTEGER,  in businessindex INTEGER,in  systemstatus INTEGER,in  providerindex INTEGER,
           in  alarmindex VARCHAR(100),in orderlevel INTEGER, in exceptfixtime INTEGER,in fixperson INTEGER, in currentstatus INTEGER,
           in  orderhandle VARCHAR(100),in handletype INTEGER)
BEGIN
select InsertWorkOrderInfo(personindex,businessindex,systemstatus,
providerindex,alarmindex,orderlevel,exceptfixtime,fixperson,currentstatus,orderhandle,handletype); END

2,2(定义函数)

CREATE  FUNCTION `InsertWorkOrderInfo`(personindex INTEGER,  businessindex INTEGER,  systemstatus INTEGER,  providerindex INTEGER,
             alarmindex VARCHAR(100), orderlevel INTEGER,  exceptfixtime INTEGER, fixperson INTEGER,  currentstatus INTEGER,
             orderhandle VARCHAR(100), handletype INTEGER) RETURNS varchar(100) CHARSET utf8
BEGIN
 declare issmsnotify INTEGER;
 declare isweixinnotify  INTEGER;
 declare createTime  DATETIME;
 declare cTime  DATETIME;
 set issmsnotify=0;
 set isweixinnotify=0;
 set cTime=now();
 set createTime=now();
 INSERT INTO M_WORKORDER (BUSINESSINDEX, SYSTEMSTATUS, PROVIDERINDEX, ALARMINDEX, ORDERLEVEL, EXCEPTFIXTIME, FIXPERSON, CURRENTSTATUS, ISSMSNOTIFY, ISWEIXINNOTIFY, CREATETIME, ORDERHANDLE, HANDLETYPE)
 VALUES (businessindex, systemstatus, providerindex, alarmindex, orderlevel, exceptfixtime, fixperson,currentstatus, issmsnotify,isweixinnotify, cTime, orderhandle, handletype);

 INSERT INTO M_ORDERHANDLE (ORDERINDEX, HANDLESTATUS, HANDLETIME, HANDLEPERSON, HANDLEDESCRIPTION, HANDLETYPE)
 VALUES (concat(‘S_CN‘,right(concat(‘00000000‘,CURRVAL(‘ENTITY_SEQ‘)),8)), currentstatus, cTime,personindex, orderhandle, handletype);
              
  RETURN concat(‘S_CN‘,right(concat(‘00000000‘,CURRVAL(‘ENTITY_SEQ‘)),8));
END

二 EF调用

 public ActionResult EditWorkOrder(FormCollection collection)
        {
            AppLog.Info("EditWorkOrder");
            try
            {
                string openID = Session["OpenID"].ToString();
                AppLog.Info("添加派工单时的openid:" + openID );
                int personindex =DBAccess.Bussiness.GetStaffIndex(openID);
                int businessindex =Convert.ToInt32(Session["PROVIDERINDEX_BUSINESSINDEX"].ToString().Split(‘/‘)[1]);//道路...index
                int systemstatus = 0;
                int providerindex = Convert.ToInt32(collection["ProviderList"]);//维护单位
                string alarmindex = Session["Alarmindex"].ToString();//报警编号有上文提供
                int orderlevel = Convert.ToInt32(collection["WorkOrderLevel"]);//优先级别
                int fixperson = Convert.ToInt32(collection["RepairUserList"]);//维修人员
                string currentstatus = Convert.ToString(collection["WorkOrderStatusList"]);//新增
                string orderhandle =string.Empty;
                if(collection["OrderHandle"]!=null)
                {
                    orderhandle = collection["OrderHandle"];//故障分类处理描述
                } 
                string handletype = Convert.ToString(collection["AlarmHandleTypeList"]);//故障处理分类
                AppLog.Info("派工单参数信息:" + "--personindex:"+personindex+ "--businessindex:"+businessindex+ "--providerindex:"+providerindex+ "--alarmindex:"+alarmindex+ "--orderlevel:"+orderlevel+ "--fixperson:"+fixperson+ "--currentstatus:"+currentstatus+"--orderhandle:"+orderhandle+"--handletype:"+handletype);
                MySqlParameter[] prams = new MySqlParameter[11];
                prams[0] = new MySqlParameter("@personindex", personindex);
                prams[1] = new MySqlParameter("@businessindex", businessindex);
                prams[2] = new MySqlParameter("@systemstatus", systemstatus);
                prams[3] = new MySqlParameter("@providerindex", providerindex);
                prams[4] = new MySqlParameter("@alarmindex", alarmindex);
                prams[5] = new MySqlParameter("@orderlevel", orderlevel);
                prams[6] = new MySqlParameter("@exceptfixtime", 24);
                prams[7] = new MySqlParameter("@fixperson", fixperson);
                prams[8] = new MySqlParameter("@currentstatus", currentstatus);
                prams[9] = new MySqlParameter("@orderhandle", orderhandle);
                prams[10] = new MySqlParameter("@handletype", handletype);
                using (cnpsim_dbEntities dbManager = new cnpsim_dbEntities())
                {
                    var index = dbManager.Database.SqlQuery<string>("call InsertWorkOrderInfo(@personindex,@businessindex,@systemstatus,@providerindex ,@alarmindex, @orderlevel,@exceptfixtime, @fixperson,@currentstatus ,@orderhandle,@handletype)", prams);
                    string ss=index.FirstOrDefault().ToString();
                    string s = ss;
                 
                    return RedirectToAction("SuccessInfo", "BackInfo", new { str1 = "成功添加派工单", str2 = "", url = "http://wx115.cnpsim.com/Business/index" });

                }
            }
            catch(Exception ex)
            {
                return null;
            }
        }

Entity Framework访问MySQL数据库的存储过程并获取返回值