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;
}
这篇关于如何将数据库查询结果转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将数据库查询结果转换为数组?
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01