mysql使用总结(C#)

当前使用环境是在mysql.data下的,mysql.data包使用nuget获取

1.连接Mysql数据库:

配置文件**.config:

 <connectionStrings>
    <add name="MySql" connectionString="Database=hyd_cpro_preview;Data Source=192.168.107.51;User Id=root;Password=cnki;CharSet=utf8;port=3306" />
  </connectionStrings>

配置连接mysql数据库字符串(从配置文件中读取):

readonly string MYSQL_CONNECT_STRING = ConfigurationManager.ConnectionStrings["MySql"].ConnectionString;

2.进行连接并查询返回结果:

string sql = $"SELECT t_fund_keyword.FundId as Code,t_fund_keyword.Keyword AS Name,t_fund_keyword.Cnt AS Count FROM t_fund_keyword " +Filter+" LIMIT 100";
using (var Con = new MySqlConnection(MYSQL_CONNECT_STRING))
{
    var result = Con.Query<StaticsEntity>(sql).ToList();
}
//对应的StaticsEntity
    public class StaticsEntity
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public int Count { get; set; }
    }

3.遍历reader

            using (var con = new MySqlConnection(MYSQL_CONNECT_STRING))
            {
                var reader = con.ExecuteReader(sql);
                while(reader.Read())
                {
                    var currentName = reader.GetName(index);
                    var currentValue = reader.GetValue(index);
                }
            }

更新mysql

发布了83 篇原创文章 · 获赞 38 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/mao_mao37/article/details/103779074
今日推荐