我怎样才能数据库查询结果转换为一个数组? [英] How can I convert database query results to an array?

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

问题描述

我将如何在MySQL查询返回的结果转换为C#.net /单声道阵列

据我了解,你需要与项目阵列将持有的数量决定阵列,但据我所知,DataReader没有告诉你有很多行返回。所以我怎么可以定义一个数组。

到目前为止,我有:

 字符串sqlWhere;

        如果((NAME =无)及!及(NAME =)!)
            sqlWhere =WHERE姓名LIKE'%+姓名+%';

        如果((公司=无)及!及(公司=)!)
            如果(sqlWhere ==)
                sqlWhere =WHERE公司LIKE'%+公司+%';
            其他
                sqlWhere = sqlWhere +和公司LIKE'%+公司+%';

        如果((dateFrom =无)及!及(dateFrom =!)及和放大器;!(dateTo =无)及和放大器;!(dateTo =))
            如果(sqlWhere ==)
                sqlWhere =WHERE日期(timestampIn)之间'+ dateFrom +'和'+ dateTo +';
            其他
                sqlWhere = sqlWhere +和日期(timestampIn)之间'+ dateFrom +'和'+ dateTo +';

        IDbCommand的dbcmd = this.dbcon.CreateCommand();
        dbcmd.CommandText =SELECT * FROM游客+ sqlWhere;

                    MySqlDataReader将读卡器= dbcmd.ExecuteReader();

                    而(Reader.Read())
            {

            }
 

解决方案

 公开的IList<组> GetGroup()
        {
            连接C =新的连接();
            字符串的connectionString = c.ConnectionName;
            OleDbConnection的康恩=新的OleDbConnection(的connectionString);
            OleDbCommand的mycmd = conn.CreateCommand();
            数据集dspendingapps =新的DataSet();
            dspendingapps.Clear();

            mycmd.CommandText =由g.groupname选择g.groupid,从tbl_group摹为了g.groupname;
            conn.Open();

            OleDbDataAdapter的AP $ P $帕德尔=新OleDbDataAdapter的(mycmd);
            AP preader.Fill(dspendingapps);
            conn.Close();

            IList的<组> G =新的名单,其中,组>();

            的foreach(在dspendingapps.Tables的DataRow drapp [0] .Rows)
            {
                集团GG =新集团();
                gg.GroupId = Convert.ToInt16(drapp [组ID]);
                。gg.Name = drapp [组名]的ToString();
                g.Add(GG);

            }
            返回克;
        }
 

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

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.

So far I have:

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天全站免登陆