.net4.0使用Dapper操作MySql


准備使用Dapper操作MySql,由於電腦只有vs2010,所以需要Dapper和MySql組件支持.net 4.0。
經過一番測試,終於弄出一個DEMO。

1、操作MySql需要用MySql.Data.dll。
MySql.Data.dll有多個版本,官網關於版本的說明鏈接如下:
https://dev.mysql.com/doc/connector-net/en/connector-net-versions.html
支持.net4.0的MySql.Data.dll的比較新版本是6.9系列。
我下載的是6.9.8版本,里面有有3個目錄v2.0、v4.0、v4.5,分別是對應.net framework版本。
把v4.0里面的MySql.Data.dll添加到vs2010的引用中。

2、Dapper的版本選擇
在這里下載的支持.net4.0
https://download.csdn.net/download/pxgame/10302588
3、Dapper封裝

public class DapperHelper
    {
        public static MySqlConnection MySqlConnection()
        {
            string mysqlConnectionStr = System.Configuration.ConfigurationManager.AppSettings["MySqlConn"].ToString();
            var connection = new MySqlConnection(mysqlConnectionStr);
            connection.Open();
            return connection;
        }
    }

其中配置文件MySqlConn的配置值為
Database=testdb;Data Source=localhost;User Id=root;Password=;CharSet=utf8;port=3306

4、User實體

public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }       
    }

5、使用Dapper進行select查詢

using (IDbConnection conn = DapperHelper.MySqlConnection())
            {               
                string sqlCommandStr = @"select * from user";
                List<User> userList = conn.Query<User>(sqlCommandStr).ToList();
               //todo
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM