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;
}
这篇关于如何将数据库查询结果转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将数据库查询结果转换为数组?
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01