Dapper常用方法總結

xuyiming888發表於2020-12-22

常用方法

複製程式碼
#region Dapper 資料庫操作

    #region Query 操作

    /// <summary>
    /// 無參獲取單個實體modle
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns>T</returns>
    public static T QueryModel<T>(string sqlstr) where T : new()
    {

        T t = new T();
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {

            try
            {
                connection.Open();
                t = connection.QueryFirstOrDefault<T>(sqlstr);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();
            }
        }

        return t;
    }


    /// <summary>
    /// 引數化獲取單個實體modle 
    /// </summary>
    /// <param name="sqlstr">執行sql語句</param>
    /// <param name="parms">引數陣列</param>
    /// <returns>T</returns>
    public static T QueryModel<T>(string sqlstr, params MySqlParameter[] parms) where T : new()
    {

        T t = new T();
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {

            try
            {
                connection.Open();
                var args = new DynamicParameters(new { });
                foreach (MySqlParameter parm in parms)
                {
                    args.Add(parm.ParameterName,parm.Value);
                }
                t = connection.QueryFirstOrDefault<T>(sqlstr,args);
                               
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();
            }
        }

        return t;
    }

    /// <summary>
    /// 獲取多個實體modle
    /// </summary>
    /// <param name="sqlString">查詢語句</param>
    /// <returns>List<T></returns>
    public static List<T> QueryModelList<T>(string sqlString) where T : new()
    {

        List<T> tlist = new List<T>();
        using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
        {

            try
            {
                connection.Open();
                tlist = connection.Query<T>(sqlString).ToList();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();
            }
            return tlist;
        }
    }

    /// <summary>
    ///  引數化獲取多個實體modle
    /// </summary>
    /// <param name="sqlString">查詢語句</param>
    /// <returns>List<T></returns>
    public static List<T> QueryModelList<T>(string sqlString, params MySqlParameter[] parms) where T : new()
    {

        List<T> tlist = new List<T>();
        using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
        {

            try
            {
                connection.Open();
                var args = new DynamicParameters(new { });
                foreach (MySqlParameter parm in parms)
                {
                    args.Add(parm.ParameterName, parm.Value);
                }
                tlist = connection.Query<T>(sqlString, args).ToList();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();
            }
            return tlist;
        }
    }


    #endregion


    #region Execute  操作

    /// <summary>
    /// 單條執行一條sql資料
    /// </summary>
    /// <param name="sqlstr">執行語句</param>
    /// <returns></returns>
    public static int Execute(string sqlstr)
    {
        int obj = 0;
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {

            try
            {
                connection.Open();
                obj = connection.Execute(sqlstr);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();

            }
        }

        return obj;
    }


    /// <summary>
    /// 引數化單條執行一條sql資料
    /// </summary>
    /// <param name="sqlstr">執行語句</param>
    /// <returns></returns>
    public static int Execute(string sqlstr, params MySqlParameter[] parms)
    {
        int obj = 0;
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {

            try
            {
                connection.Open();
                var args = new DynamicParameters(new { });
                foreach (MySqlParameter parm in parms)
                {
                    args.Add(parm.ParameterName, parm.Value);
                }
                obj = connection.Execute(sqlstr, args);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                //throw new Exception(ex.Message);
                connection.Close();

            }
        }

        return obj;
    }






    #endregion




    #endregion

https://www.github.com/users/hbfs25505/projects/441
https://github.com/users/hbfs25505/projects/442
https://www.github.com/users/hbfs25505/projects/442
https://github.com/users/hbfs25505/projects/443
https://www.github.com/users/hbfs25505/projects/443
https://github.com/users/hbfs25505/projects/444
https://www.github.com/users/hbfs25505/projects/444
https://github.com/users/hbfs25505/projects/445
https://www.github.com/users/hbfs25505/projects/445
https://github.com/users/hbfs25505/projects/446
https://www.github.com/users/hbfs25505/projects/446
https://github.com/users/hbfs25505/projects/447
https://www.github.com/users/hbfs25505/projects/447
https://github.com/users/hbfs25505/projects/448
https://www.github.com/users/hbfs25505/projects/448
https://github.com/users/hbfs25505/projects/449
https://www.github.com/users/hbfs25505/projects/449
https://github.com/users/hbfs25505/projects/450
https://www.github.com/users/hbfs25505/projects/450
https://github.com/users/hbfs25505/projects/451
https://www.github.com/users/hbfs25505/projects/451
https://github.com/users/hbfs25505/projects/452
https://www.github.com/users/hbfs25505/projects/452
https://github.com/users/hbfs25505/projects/453
https://www.github.com/users/hbfs25505/projects/453
https://github.com/users/hbfs25505/projects/454
https://www.github.com/users/hbfs25505/projects/454
https://github.com/users/hbfs25505/projects/455
https://www.github.com/users/hbfs25505/projects/455
https://github.com/users/hbfs25505/projects/456
https://www.github.com/users/hbfs25505/projects/456
https://github.com/users/hbfs25505/projects/457
https://www.github.com/users/hbfs25505/projects/457
https://github.com/users/hbfs25505/projects/458
https://www.github.com/users/hbfs25505/projects/458
https://github.com/users/hbfs25505/projects/459
https://www.github.com/users/hbfs25505/projects/459
https://github.com/users/hbfs25505/projects/460
https://www.github.com/users/hbfs25505/projects/460
https://github.com/users/hbfs25505/projects/461
https://www.github.com/users/hbfs25505/projects/461
https://github.com/users/hbfs25505/projects/462
https://www.github.com/users/hbfs25505/projects/462
https://github.com/users/hbfs25505/projects/463
https://www.github.com/users/hbfs25505/projects/463
https://github.com/users/hbfs25505/projects/464
https://www.github.com/users/hbfs25505/projects/464
https://github.com/users/hbfs25505/projects/465
https://www.github.com/users/hbfs25505/projects/465
https://github.com/users/hbfs25505/projects/466
https://www.github.com/users/hbfs25505/projects/466
https://github.com/users/hbfs25505/projects/467
https://www.github.com/users/hbfs25505/projects/467
https://github.com/users/hbfs25505/projects/468
https://www.github.com/users/hbfs25505/projects/468
https://github.com/users/hbfs25505/projects/469
https://www.github.com/users/hbfs25505/projects/469
https://github.com/users/hbfs25505/projects/470
https://www.github.com/users/hbfs25505/projects/470
https://github.com/users/hbfs25505/projects/471
https://www.github.com/users/hbfs25505/projects/471
https://github.com/users/hbfs25505/projects/472
https://www.github.com/users/hbfs25505/projects/472
https://github.com/users/hbfs25505/projects/473
https://www.github.com/users/hbfs25505/projects/473
https://github.com/users/hbfs25505/projects/474
https://www.github.com/users/hbfs25505/projects/474
https://github.com/users/hbfs25505/projects/475
https://www.github.com/users/hbfs25505/projects/475
https://github.com/users/hbfs25505/projects/476
https://www.github.com/users/hbfs25505/projects/476
https://github.com/users/hbfs25505/projects/477
https://www.github.com/users/hbfs25505/projects/477
https://github.com/users/hbfs25505/projects/478
https://www.github.com/users/hbfs25505/projects/478
https://github.com/users/hbfs25505/projects/479
https://www.github.com/users/hbfs25505/projects/479
https://github.com/users/hbfs25505/projects/480
https://www.github.com/users/hbfs25505/projects/480
https://github.com/users/hbfs25505/projects/481
https://www.github.com/users/hbfs25505/projects/481
https://github.com/users/hbfs25505/projects/482
https://www.github.com/users/hbfs25505/projects/482
https://github.com/users/hbfs25505/projects/483
https://www.github.com/users/hbfs25505/projects/483
https://github.com/users/hbfs25505/projects/484
https://www.github.com/users/hbfs25505/projects/484
https://github.com/users/hbfs25505/projects/485
https://www.github.com/users/hbfs25505/projects/485
https://github.com/users/hbfs25505/projects/486
https://www.github.com/users/hbfs25505/projects/486
https://github.com/users/hbfs25505/projects/487
https://www.github.com/users/hbfs25505/projects/487
https://github.com/users/hbfs25505/projects/488
https://www.github.com/users/hbfs25505/projects/488
https://github.com/users/hbfs25505/projects/489
https://www.github.com/users/hbfs25505/projects/489
https://github.com/users/hbfs25505/projects/490
https://www.github.com/users/hbfs25505/projects/490
https://github.com/users/hbfs25505/projects/491
https://www.github.com/users/hbfs25505/projects/491
https://github.com/users/hbfs25505/projects/492
https://www.github.com/users/hbfs25505/projects/492
https://github.com/users/hbfs25505/projects/493
https://www.github.com/users/hbfs25505/projects/493
https://github.com/users/hbfs25505/projects/494
https://www.github.com/users/hbfs25505/projects/494
https://github.com/users/hbfs25505/projects/495
https://www.github.com/users/hbfs25505/projects/495
https://github.com/users/hbfs25505/projects/496
https://www.github.com/users/hbfs25505/projects/496
https://github.com/users/hbfs25505/projects/497
https://www.github.com/users/hbfs25505/projects/497
https://github.com/users/hbfs25505/projects/498
https://www.github.com/users/hbfs25505/projects/498
https://github.com/users/hbfs25505/projects/499
https://www.github.com/users/hbfs25505/projects/499
https://github.com/users/hbfs25505/projects/500
https://www.github.com/users/hbfs25505/projects/500
https://github.com/users/hbfs25505/projects/501
https://www.github.com/users/hbfs25505/projects/501
https://github.com/users/hbfs25505/projects/502
https://www.github.com/users/hbfs25505/projects/502
https://github.com/users/hbfs25505/projects/503
https://www.github.com/users/hbfs25505/projects/503
https://github.com/users/hbfs25505/projects/504
https://www.github.com/users/hbfs25505/projects/504
https://github.com/users/hbfs25505/projects/505
https://www.github.com/users/hbfs25505/projects/505
https://github.com/users/hbfs25505/projects/506
https://www.github.com/users/hbfs25505/projects/506
https://github.com/users/hbfs25505/projects/507
https://www.github.com/users/hbfs25505/projects/507
https://github.com/users/hbfs25505/projects/508
https://www.github.com/users/hbfs25505/projects/508
https://github.com/users/hbfs25505/projects/509
https://www.github.com/users/hbfs25505/projects/509
https://github.com/users/hbfs25505/projects/510
https://www.github.com/users/hbfs25505/projects/510
https://github.com/users/hbfs25505/projects/511
https://www.github.com/users/hbfs25505/projects/511
https://github.com/users/hbfs25505/projects/512
https://www.github.com/users/hbfs25505/projects/512
https://github.com/users/hbfs25505/projects/513
https://www.github.com/users/hbfs25505/projects/513
https://github.com/users/hbfs25505/projects/514
https://www.github.com/users/hbfs25505/projects/514
https://github.com/users/hbfs25505/projects/515
https://www.github.com/users/hbfs25505/projects/515
https://github.com/users/hbfs25505/projects/516
https://www.github.com/users/hbfs25505/projects/516
https://github.com/users/hbfs25505/projects/517
https://www.github.com/users/hbfs25505/projects/517
https://github.com/users/hbfs25505/projects/518
https://www.github.com/users/hbfs25505/projects/518
https://github.com/users/hbfs25505/projects/519
https://www.github.com/users/hbfs25505/projects/519
https://github.com/users/hbfs25505/projects/520
https://www.github.com/users/hbfs25505/projects/520
https://github.com/users/hbfs25505/projects/521
https://www.github.com/users/hbfs25505/projects/521
https://github.com/users/hbfs25505/projects/522
https://www.github.com/users/hbfs25505/projects/522
https://github.com/users/hbfs25505/projects/523
https://www.github.com/users/hbfs25505/projects/523
https://github.com/users/hbfs25505/projects/524
https://www.github.com/users/hbfs25505/projects/524
https://github.com/users/hbfs25505/projects/525
https://www.github.com/users/hbfs25505/projects/525
https://github.com/users/hbfs25505/projects/526
https://www.github.com/users/hbfs25505/projects/526
https://github.com/users/hbfs25505/projects/527
https://www.github.com/users/hbfs25505/projects/527
https://github.com/users/hbfs25505/projects/528
https://www.github.com/users/hbfs25505/projects/528
https://github.com/users/hbfs25505/projects/529
https://www.github.com/users/hbfs25505/projects/529
https://github.com/users/hbfs25505/projects/530
https://www.github.com/users/hbfs25505/projects/530
https://github.com/users/hbfs25505/projects/531
https://www.github.com/users/hbfs25505/projects/531
https://github.com/users/hbfs25505/projects/532
https://www.github.com/users/hbfs25505/projects/532
https://github.com/users/hbfs25505/projects/533
https://www.github.com/users/hbfs25505/projects/533
https://github.com/users/hbfs25505/projects/534
https://www.github.com/users/hbfs25505/projects/534
https://github.com/users/hbfs25505/projects/535
https://www.github.com/users/hbfs25505/projects/535
https://github.com/users/hbfs25505/projects/536
https://www.github.com/users/hbfs25505/projects/536
https://github.com/users/hbfs25505/projects/537
https://www.github.com/users/hbfs25505/projects/537
https://github.com/users/hbfs25505/projects/538
https://www.github.com/users/hbfs25505/projects/538
https://github.com/users/hbfs25505/projects/539
https://www.github.com/users/hbfs25505/projects/539
https://github.com/users/hbfs25505/projects/540
https://www.github.com/users/hbfs25505/projects/540
https://github.com/users/hbfs25505/projects/541
https://www.github.com/users/hbfs25505/projects/541
https://github.com/users/hbfs25505/projects/542
https://www.github.com/users/hbfs25505/projects/542
https://github.com/users/hbfs25505/projects/543
https://www.github.com/users/hbfs25505/projects/543
https://github.com/users/hbfs25505/projects/544
https://www.github.com/users/hbfs25505/projects/544
https://github.com/users/hbfs25505/projects/545
https://www.github.com/users/hbfs25505/projects/545
https://github.com/users/hbfs25505/projects/546
https://www.github.com/users/hbfs25505/projects/546
https://github.com/users/hbfs25505/projects/547
https://www.github.com/users/hbfs25505/projects/547
https://github.com/users/hbfs25505/projects/548
https://www.github.com/users/hbfs25505/projects/548
https://github.com/users/hbfs25505/projects/549
https://www.github.com/users/hbfs25505/projects/549
https://github.com/users/hbfs25505/projects/550
https://www.github.com/users/hbfs25505/projects/550
https://github.com/users/hbfs25505/projects/551
https://www.github.com/users/hbfs25505/projects/551
https://github.com/users/hbfs25505/projects/552
https://www.github.com/users/hbfs25505/projects/552
https://github.com/users/hbfs25505/projects/553
https://www.github.com/users/hbfs25505/projects/553
https://github.com/users/hbfs25505/projects/554
https://www.github.com/users/hbfs25505/projects/554
https://github.com/users/hbfs25505/projects/555
https://www.github.com/users/hbfs25505/projects/555
https://github.com/users/hbfs25505/projects/556
https://www.github.com/users/hbfs25505/projects/556
https://github.com/users/hbfs25505/projects/557
https://www.github.com/users/hbfs25505/projects/557
https://github.com/users/hbfs25505/projects/558
https://www.github.com/users/hbfs25505/projects/558
https://github.com/users/hbfs25505/projects/559
https://www.github.com/users/hbfs25505/projects/559
https://github.com/users/hbfs25505/projects/560
https://www.github.com/users/hbfs25505/projects/560
https://github.com/users/hbfs25505/projects/561
https://www.github.com/users/hbfs25505/projects/561
https://github.com/users/hbfs25505/projects/562
https://www.github.com/users/hbfs25505/projects/562
https://github.com/users/hbfs25505/projects/563
https://www.github.com/users/hbfs25505/projects/563
https://github.com/users/hbfs25505/projects/564
https://www.github.com/users/hbfs25505/projects/564
https://github.com/users/hbfs25505/projects/565
https://www.github.com/users/hbfs25505/projects/565
https://github.com/users/hbfs25505/projects/566
https://www.github.com/users/hbfs25505/projects/566
https://github.com/users/hbfs25505/projects/567
https://www.github.com/users/hbfs25505/projects/567
https://github.com/users/hbfs25505/projects/568
https://www.github.com/users/hbfs25505/projects/568
https://github.com/users/hbfs25505/projects/569
https://www.github.com/users/hbfs25505/projects/569
https://github.com/users/hbfs25505/projects/570
https://www.github.com/users/hbfs25505/projects/570
https://github.com/users/hbfs25505/projects/571
https://www.github.com/users/hbfs25505/projects/571
https://github.com/users/hbfs25505/projects/572
https://www.github.com/users/hbfs25505/projects/572
https://github.com/users/hbfs25505/projects/573
https://www.github.com/users/hbfs25505/projects/573
https://github.com/users/hbfs25505/projects/574
https://www.github.com/users/hbfs25505/projects/574
https://github.com/users/hbfs25505/projects/575
https://www.github.com/users/hbfs25505/projects/575
https://github.com/users/hbfs25505/projects/576
https://www.github.com/users/hbfs25505/projects/576
https://github.com/users/hbfs25505/projects/577
https://www.github.com/users/hbfs25505/projects/577
https://github.com/users/hbfs25505/projects/578
https://www.github.com/users/hbfs25505/projects/578
https://github.com/users/hbfs25505/projects/579
https://www.github.com/users/hbfs25505/projects/579
https://github.com/users/hbfs25505/projects/580
https://www.github.com/users/hbfs25505/projects/580
https://github.com/users/hbfs25505/projects/581
https://www.github.com/users/hbfs25505/projects/581
https://github.com/users/hbfs25505/projects/582
https://www.github.com/users/hbfs25505/projects/582
https://github.com/users/hbfs25505/projects/583
https://www.github.com/users/hbfs25505/projects/583
https://github.com/users/hbfs25505/projects/584
https://www.github.com/users/hbfs25505/projects/584
https://github.com/users/hbfs25505/projects/585
https://www.github.com/users/hbfs25505/projects/585
https://github.com/users/hbfs25505/projects/586
https://www.github.com/users/hbfs25505/projects/586
https://github.com/users/hbfs25505/projects/587
https://www.github.com/users/hbfs25505/projects/587
https://github.com/users/hbfs25505/projects/588
https://www.github.com/users/hbfs25505/projects/588
https://github.com/users/hbfs25505/projects/589
https://www.github.com/users/hbfs25505/projects/589
https://github.com/users/hbfs25505/projects/590
https://www.github.com/users/hbfs25505/projects/590
https://github.com/users/hbfs25505/projects/591
https://www.github.com/users/hbfs25505/projects/591
https://github.com/users/hbfs25505/projects/592
https://www.github.com/users/hbfs25505/projects/592
https://github.com/users/hbfs25505/projects/593
https://www.github.com/users/hbfs25505/projects/593
https://github.com/users/hbfs25505/projects/594
https://www.github.com/users/hbfs25505/projects/594
https://github.com/users/hbfs25505/projects/595
https://www.github.com/users/hbfs25505/projects/595
https://github.com/users/hbfs25505/projects/596
https://www.github.com/users/hbfs25505/projects/596
https://github.com/users/hbfs25505/projects/597
https://www.github.com/users/hbfs25505/projects/597
https://github.com/users/hbfs25505/projects/598
https://www.github.com/users/hbfs25505/projects/598
https://github.com/users/hbfs25505/projects/599
https://www.github.com/users/hbfs25505/projects/599
https://github.com/users/hbfs25505/projects/600
https://www.github.com/users/hbfs25505/projects/600
https://github.com/users/hbfs25505/projects/601
https://www.github.com/users/hbfs25505/projects/601
https://github.com/users/hbfs25505/projects/602
https://www.github.com/users/hbfs25505/projects/602
https://github.com/users/hbfs25505/projects/603
https://www.github.com/users/hbfs25505/projects/603
https://github.com/users/hbfs25505/projects/604
https://www.github.com/users/hbfs25505/projects/604
https://github.com/users/hbfs25505/projects/605
https://www.github.com/users/hbfs25505/projects/605
https://github.com/users/hbfs25505/projects/606
https://www.github.com/users/hbfs25505/projects/606
https://github.com/users/hbfs25505/projects/607
https://www.github.com/users/hbfs25505/projects/607
https://github.com/users/hbfs25505/projects/608
https://www.github.com/users/hbfs25505/projects/608
https://github.com/users/hbfs25505/projects/609
https://www.github.com/users/hbfs25505/projects/609
https://github.com/users/hbfs25505/projects/610
https://www.github.com/users/hbfs25505/projects/610
https://github.com/users/hbfs25505/projects/611
https://www.github.com/users/hbfs25505/projects/611
https://github.com/users/hbfs25505/projects/612
https://www.github.com/users/hbfs25505/projects/612
https://github.com/users/hbfs25505/projects/613
https://www.github.com/users/hbfs25505/projects/613
https://github.com/users/hbfs25505/projects/614
https://www.github.com/users/hbfs25505/projects/614
https://github.com/users/hbfs25505/projects/615
https://www.github.com/users/hbfs25505/projects/615
https://github.com/users/hbfs25505/projects/616
https://www.github.com/users/hbfs25505/projects/616
https://github.com/users/hbfs25505/projects/617
https://www.github.com/users/hbfs25505/projects/617
https://github.com/users/hbfs25505/projects/618
https://www.github.com/users/hbfs25505/projects/618
https://github.com/users/hbfs25505/projects/619
https://www.github.com/users/hbfs25505/projects/619
https://github.com/users/hbfs25505/projects/620
https://www.github.com/users/hbfs25505/projects/620
https://github.com/users/hbfs25505/projects/621
https://www.github.com/users/hbfs25505/projects/621
https://github.com/users/hbfs25505/projects/622
https://www.github.com/users/hbfs25505/projects/622
https://github.com/users/hbfs25505/projects/623

相關文章