抛出异常:CRUD中MySql.Data.dll中的“MySql.Data.MySqlClient.MySqlException”

时间:2016-04-15 07:15:38

标签: c# mysql asp.net-mvc-4 crud dapper

问候!

我正在使用dapper ORM构建MVC应用程序,但是存在问题。在创建方法和编辑中,我看不到插入或编辑的新数据。我想知道它的原因。如果有人能说出我在哪里做错了吗?

在调试模式下,当它到达返回RedirectToAction(“索引”)时它告诉和异常:抛出异常:MySql.Data.dll中的'MySql.Data.MySqlClient.MySqlException' 。有人能解释一下这个含义吗

以下是业务/数据层:

public string Create(UserActivityModels activityEntity)
    {
        string sqlQuery =
@"INSERT INTO UserActivity (CreatedBy, CreatedOn, 
ModifiedBy, ModifiedOn, EntityName, EntityId, StatusCode,
StateCode, ActivityType, ActivityStatus, DueDate,ActualEndDate,MasqueradeOn, MasqueradeBy)
 VALUES (@CreatedBy,@CreatedOn,@ModifiedBy,@ModifiedOn, @EntityName, @EntityId, @StatusCode, @StateCode, @ActivityType, @ActivityStatus, @DueDate, @ActualEndDate, @MasqueradeOn, @MasqueradeBy)
";
        try
        {
            using (IDbConnection db = new MySqlConnection
             (ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].
             ConnectionString))
            {
              var  userActivity = (List<UserActivityModels>)db.Query<UserActivityModels>(sqlQuery, 
                  new
                  {
                                          activityEntity.CreatedBy,
                                          activityEntity.CreatedOn,
                                          activityEntity.ModifiedBy,
                                          activityEntity.ModifiedOn,
                                          activityEntity.EntityName,
                                          activityEntity.EntityId,
                                          activityEntity.StatusCode,
                                          activityEntity.StateCode,
                                          activityEntity.ActivityType,
                                          activityEntity.ActivityStatus,
                                          activityEntity.DueDate,
                                          activityEntity.ActualEndDate,
                                          activityEntity.MasqueradeOn,
                                          activityEntity.MasqueradeBy
                                      });

                db.Close();

            }
            return "Created";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }



    public bool Edit(UserActivityModels activityEntity)
    {
        string sqlQuery =
  @"UPDATE UserActivity SET CreatedBy =@CreatedBy, CreatedOn =@CreatedOn, 
ModifiedBy =@ModifiedBy, ModifiedOn = @ModifiedOn, EntityName =@EntityName, 
EntityId=@EntityId, StatusCode =@StatusCode, StateCode=@StateCode, ActivityType=@ActivityType, ActivityStatus=@ActivityStatus, DueDate=@DueDate, ActualEndDate=@ActualEndDate, MasqueradeOn=@MasqueradeOn,MasqueradeBy=@MasqueradeBy 
   WHERE Id=@UserActivityId
";
        try
        {
            using (IDbConnection db = new MySqlConnection
            (ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].
            ConnectionString))
            {
                var userActivity = (List<UserActivityModels>)db.Query<UserActivityModels>(sqlQuery,
           new
            {
                    activityEntity.CreatedBy,
                    activityEntity.CreatedOn,
                    activityEntity.ModifiedBy,
                    activityEntity.ModifiedOn,
                    activityEntity.EntityName,
                    activityEntity.EntityId,
                    activityEntity.StatusCode,
                    activityEntity.StateCode,
                    activityEntity.ActivityType,
                    activityEntity.ActivityStatus,
                    activityEntity.DueDate,
                    activityEntity.ActualEndDate,
                    activityEntity.MasqueradeOn,
                    activityEntity.MasqueradeBy
                });
                db.Close();

            }
            return true;
        }
        catch (Exception exception)
        {
            return false;
        }
    }

控制器:

    //GET /UserActivity/Create
    public ActionResult Create()
    {
        return View();
    }
    //
    // POST: /UserActivity/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(UserActivityModels models)
    {
        try
        {
            // TODO: Add insert logic here
            var activityEntities = new ActivityLogic();
            activityEntities.Create(models);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

与基本控制器有关系吗?

 public class BaseController : Controller
 {
 protected override void  OnActionExecuting(ActionExecutingContextfilterContext)
    {
         if (filterContext.HttpContext.Session["User"] == null)
         {
            //filterContext.Result = RedirectToAction("Login","Account");
            //filterContext.Result = RedirectToAction("Index");
         }

         CultureHelper.CurrentCulture = 10;  
         }
         }

非常感谢信息

0 个答案:

没有答案