How can I convert database query results to an array?

前端 未结 4 600
一向
一向 2021-01-23 07:59

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 wi

4条回答
  •  孤城傲影
    2021-01-23 08:27

    public IList 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 g = new List();
    
                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;
            }
    

提交回复
热议问题