简体   繁体   English

将查询结果存储在字典中

[英]Store Result of a Query in a Dictionary

I have problem to develop Windows mobile app using C#, I trying to work with 3 query. 我在使用C#开发Windows移动应用程序时遇到问题,我尝试使用3查询。 I want to collect all result query into Dictionary. 我想将所有结果查询收集到Dictionary中。

can i collect it without looping? 我可以不循环地收集它吗?

like this my code: 像这样我的代码:

string query= "select distinct nameProduct, price from product";
SqlCeCommand cmd = new SqlCeCommand(sql, dbConn);
Dictionary production = new Dictionary<string, int>();

How can i set this production from my query, becausei have 3 query like this in one process and the data is more than 10.000. 我如何从查询中设置此产量,因为我在一个过程中有3个这样的查询,并且数据超过10.000。

@John Saunders: when i try this, i get error"{"No data exists for the row/column."}" but actually when i try the query in sql server still running well. @John Saunders:当我尝试这样做时,出现错误“ {”行/列中没有数据。“}”,但实际上,当我尝试在sql server中查询时仍然运行良好。 the error in the line: production[(string)reader["nameProduct"]] = (int)reader["price"]; 该行中的错误:production [(string)reader [“ nameProduct”]] =(int)reader [“ price”];

@Johnny: unfortunatelly this project build in VS2005 and not support union and join statement. @Johnny:很遗憾,此项目在VS2005中构建,不支持union和join语句。

Dictionary production = new Dictionary<string, int>();
string query= "select distinct nameProduct, price from product";
using (SqlCeCommand cmd = new SqlCeCommand(sql, dbConn))
{
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            production[(string)reader["nameProduct"]] = (int)reader["price"];
        }
    }
}

Without a loop. 没有循环。 Note I haven't tried it: 注意,我还没有尝试过:

production =
    reader.Cast<IDataRecord>()
          .ToDictionary(
              record => (string)record["nameProduct"], 
              record => (int)record["price"]);

This is the simple way to fetch data from database and store data in dictionary 这是从数据库中获取数据并将数据存储在字典中的简单方法

Dictionary<int, string>emp = new Dictionary<int, string>();
OleDbCommand cmd = new OleDbCommand("select empId,Name from Employee where ORDER BY empId DESC", con);
OleDbDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
    while (dr.Read())
    {
         emp.Add(Convert.ToInt32(dr["empId"]), dr["Name"].ToString());                
    }                    
}

foreach (KeyValuePair<int, string> em in emp)
{
    Console.WriteLine(em.Key.ToString() + " = " + em.Value);
}

You may convert DataTable (use DataAdapter.Fill method to populate the DataTable) result into Dictionay. 您可以将DataTable(使用DataAdapter.Fill方法填充DataTable)结果转换为Dictionay。

string query= "select distinct nameProduct, price from product";
SqlCeConnection conn = new SqlCeConnection("Data Source = your file.sdf");

SqlCeCommand selectCmd = conn.CreateCommand();
selectCmd.CommandText = query;

SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);
DataTable dt=new DataTable();
adp.Fill(dt);

Dictionary<string, int> result = (from row in dt.AsEnumerable()
                          select new KeyValuePair<string, int>
                          (row.Field<string>("nameProduct"), row.Field<int>("price")))
                          .ToDictionary(p=>p.Key,p=>p.Value);

 foreach (var t in result)
  {
    Console.WriteLine(t.Key + " " + t.Value);
  }

A few points. 几点。

1.I think you can use "union all" in your query. 1.我认为您可以在查询中使用“全部联盟”。 2.If you have more than 10000 rows of record,especially on a mobile device, I will suggest you to only load those data you need, and you can display with in one screen, instead of read all. 2.如果您有超过10000行的记录,尤其是在移动设备上,我建议您仅加载所需的数据,并且可以在一个屏幕中显示而不是全部读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM