如何将数据库查询结果转换为数组?

How can I convert database query results to an array?(如何将数据库查询结果转换为数组?)
本文介绍了如何将数据库查询结果转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 MySQL 查询中返回的结果转换为 C# .Net/Mono 中的数组

How would I convert the results returned in a MySQL query to an array in C# .Net/Mono

据我了解,您需要使用数组将包含的项目数来定义数组,但我知道 DataReader 并没有告诉您返回了很多行.那么如何定义一个数组.

To my understanding you need to define arrays with the number of items the array will hold but I understand that the DataReader doesn't tell you have many row was returned. so how can I define a array.

到目前为止我有:

string sqlWhere;

        if ((name != None) && (name != ""))
            sqlWhere = "WHERE name LIKE '%"+name+"%'";

        if ((company != None) && (company != ""))
            if (sqlWhere == "")
                sqlWhere = "WHERE company LIKE '%"+company+"%'";
            else
                sqlWhere = sqlWhere + " AND company LIKE '%"+company+"%'";

        if ((dateFrom != None) && (dateFrom != "") && (dateTo != None) && (dateTo != ""))
            if (sqlWhere == "")
                sqlWhere = "WHERE date(timestampIn) BETWEEN '"+dateFrom+"' AND '"+dateTo+"'";
            else
                sqlWhere = sqlWhere + " AND date(timestampIn) BETWEEN '"+dateFrom+"' AND '"+dateTo+"'";

        IDbCommand dbcmd = this.dbcon.CreateCommand();
        dbcmd.CommandText = "SELECT * FROM visitors " + sqlWhere;

                    MySqlDataReader Reader = dbcmd.ExecuteReader();

                    while (Reader.Read())
            {

            }

推荐答案

public IList<Group> GetGroup()
        {
            Connection c = new Connection();
            String connectionString = c.ConnectionName;
            OleDbConnection conn = new OleDbConnection(connectionString);
            OleDbCommand mycmd = conn.CreateCommand();
            DataSet dspendingapps = new DataSet();
            dspendingapps.Clear();

            mycmd.CommandText = " select g.groupid,g.groupname from tbl_group g order by g.groupname ";
            conn.Open();

            OleDbDataAdapter appreader = new OleDbDataAdapter(mycmd);
            appreader.Fill(dspendingapps);
            conn.Close();

            IList<Group> g = new List<Group>();

            foreach (DataRow drapp in dspendingapps.Tables[0].Rows)
            {
                Group gg = new Group();
                gg.GroupId = Convert.ToInt16(drapp["groupid"]);
                gg.Name = drapp["groupname"].ToString();
                g.Add(gg);

            }
            return g;
        }

这篇关于如何将数据库查询结果转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)