【问题标题】:how to return MySqlDataReader reader result into a List<DateTime> mylist如何将 MySqlDataReader 阅读器结果返回到 List<DateTime> mylist
【发布时间】:2018-05-15 02:02:10
【问题描述】:

首先,我使用 db 方法来检索数据库记录

public void db()
{
    string connString = "Server=localhost;Database=test;Uid=root;Pwd='';SslMode=none";

    using (MySqlConnection mcon = new MySqlConnection(connString))
    using (MySqlCommand cmd = mcon.CreateCommand())
    {
        mcon.Open();

        cmd.CommandText = "SELECT dates FROM abctable";

        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                result.Text += reader.GetString(reader.GetOrdinal("dates"));
            }
        }
    }
}

现在,我可以在日期列中获取两条数据库记录 2018050120180503。如何将这些记录放入List&lt;DateTime&gt; mylist

目前,日期只是硬编码

例如

 List<DateTime> mylist = new List<DateTime> { DateTime.ParseExact("20180509", "yyyyMMdd", CultureInfo.InvariantCulture) };

有什么想法吗?谢谢

无法下载 NuGet:

【问题讨论】:

  • 不要将日期时间、整数(或任何其他非字符串)作为字符串存储在数据库中,除非它是字符串,这是您当天的友好提示
  • 您可能还想查看Dapper 框架。

标签: c# mysql asp.net


【解决方案1】:

使用Dapper可以简化对数据库的访问

public List<DateTime> getDates() {
    string connString = "Server=localhost;Database=test;Uid=root;Pwd='';SslMode=none";
    using (var connection = new MySqlConnection(connString)) {
        return connection.Query<DateTime>("SELECT dates FROM abctable").ToList();            
    }
}

假设abctable中的dates作为实际日期存储在数据库中。

然后通过调用该函数来填充您的列表。

List<DateTime> mylist = getDates();

【讨论】:

  • @epiphany 应该可以通过 IDE 中的 nuget 获取它nuget.org/packages/Dapper
  • 我可以用visual studio下载吗
  • @epiphany 视觉工作室是 IDE。所以是的,你可以通过 nuget 在 Visual Studio 中获得它
  • @epiphany No. nuget 是您展示的图像。输入 Dapper 它应该过滤掉它
  • Mysql.Data.MysqlClient.MysqlConnection 不包含“查询”的定义,并且找不到任何扩展方法“查询”
【解决方案2】:

试试这个课程:

public static class DB
{
    private static readonly string ConnectionString = "Server=localhost;Database=test;Uid=root;Pwd='';SslMode=none";

    private static IEnumerable<T> GetData(string sql, Action<MySqlParameterCollection> SetParameters, Func<IDataRecord, T> transformRecord)
    {
        using (var mcon = new MySqlConnection(ConnectionString))
        using (var cmd = new MySqlCommand(sql, mcon))
        {
            if (SetParameters != null) SetParameters(cmd.Parameters);
            using (var rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    yield return transformRecord(rdr);
                }
                rdr.Close();
            }
        }   
    }

    public static IEnumerable<DateTime> GetDates()
    {           
        return GetData("SELECT dates FROM abctable", null,
             r => (DateTime)r["dates"]);
    }
}

像这样使用它:

var myList = DB.GetDates().ToList();

您可以向其添加其他公共方法,这些方法遵循与GetDates() 相同的模式,用于读取数据库中的不同表。下面是一个示例,展示了如何使用 SetParameters 参数来使用准备好的语句/参数化查询:

public IEnumerable<DateTime> GetDates(int ID)
{           
    return GetData("SELECT dates FROM abctable WHERE ID= @ID", 
         p => {
            p.Add("@ID", MySqlDbType.Int32).Value = ID;
         },
         r => (DateTime)r["dates"]);
}

您还需要一个额外的私有方法来支持 INSERT/UPDATE/DELETE 语句,但我已经在这里展示了足够多的内容,您现在应该可以自己编写它了。

【讨论】:

  • 新手在这里,注意解释更多关于如何实现它,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多