如何将数据库查询结果转换为数组? [英] How can I convert database query results to an array?

查看:18
本文介绍了如何将数据库查询结果转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 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;
        }

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆