如何保存从查询中检索到的数据

发布于 2024-11-09 05:29:09 字数 1587 浏览 0 评论 0原文

我之前问过这个问题并得到了答案 编写查询的最佳方法,但问题是,如果您必须将此结果保存在列表中,则会出现重复的记录。例如 给定连接的结果表 示例

看到有重复的行。如何过滤掉它们,同时保存订单号数据? 当然可能有一些方法,但我正在寻找一些好方法,

我们如何将数据存储在列表中而不在列表中创建重复的行?

我当前的表代码是

  int lastUserId = 0;
  sql_cmd = new SqlCommand();
  sql_cmd.Connection = sql_con;

  sql_cmd.CommandText = "SELECT * FROM AccountsUsers LEFT JOIN Accounts ON AccountsUsers.Id = Accounts.userId ORDER BY AccountsUsers.accFirstName";
  SqlDataReader reader = sql_cmd.ExecuteReader();

  if (reader.HasRows == true)
  {
      Users userToAdd = new Users();

      while (reader.Read())
      {
           userToAdd = new Users();
           userToAdd.userId = int.Parse(reader["Id"].ToString());
           userToAdd.firstName = reader["accFirstName"].ToString();
           userToAdd.lastName = reader["accLastName"].ToString();
           lastUserId = userToAdd.userId;

           Websites domainData = new Websites();
           domainData.domainName = reader["accDomainName"].ToString();
           domainData.userName = reader["accUserName"].ToString();
           domainData.password = reader["accPass"].ToString();
           domainData.URL = reader["accDomain"].ToString();
           userToAdd.DomainData.Add(domainData);

           allUsers.Add(userToAdd);
       }
    }

对于第二个表,我有自定义列表,它将保存第二个表中所有数据的条目。

返回的表是具有联接的表,并且具有相同的多行

I previously asked the question and got answer to Best approach to write query but the problem is that if you have to save this result in a list then there duplication of records. For example
the resultant table of the join given EXAMPLE

See there are duplicate rows. How can you filter them out, and yet save the data of order number?
Of course there may be some ways but I am looking for some great ways

How can we store the data in list and not create duplicate rows in list?

My current code for my tables is

  int lastUserId = 0;
  sql_cmd = new SqlCommand();
  sql_cmd.Connection = sql_con;

  sql_cmd.CommandText = "SELECT * FROM AccountsUsers LEFT JOIN Accounts ON AccountsUsers.Id = Accounts.userId ORDER BY AccountsUsers.accFirstName";
  SqlDataReader reader = sql_cmd.ExecuteReader();

  if (reader.HasRows == true)
  {
      Users userToAdd = new Users();

      while (reader.Read())
      {
           userToAdd = new Users();
           userToAdd.userId = int.Parse(reader["Id"].ToString());
           userToAdd.firstName = reader["accFirstName"].ToString();
           userToAdd.lastName = reader["accLastName"].ToString();
           lastUserId = userToAdd.userId;

           Websites domainData = new Websites();
           domainData.domainName = reader["accDomainName"].ToString();
           domainData.userName = reader["accUserName"].ToString();
           domainData.password = reader["accPass"].ToString();
           domainData.URL = reader["accDomain"].ToString();
           userToAdd.DomainData.Add(domainData);

           allUsers.Add(userToAdd);
       }
    }

For second table I have custom list that will hold the entries of all the data in second table.

The table returned is table having joins and have multiple rows for same

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没企图 2024-11-16 05:29:09

除了使用 Antonio Bakula 回答的字典想法之外...

如果您保留用户字典并多次调用示例中的代码,您应该考虑用户帐户是新的、修改的或删除的。

执行 SQL 查询时使用的算法如下:

  1. 如果查询结果中的行不在字典中,则创建新用户并将其添加到字典中。
  2. 如果查询结果中的行在字典中,则更新用户信息。
  3. 如果字典项不在查询结果中,则从字典中删除该用户。

我还建议不要使用 SELECT *
仅使用代码所需的表列,这可以提高代码的性能,并通过返回私有用户信息来防止潜在的安全漏洞。

Besides using the Dictionary idea as answered by Antonio Bakula...

If you persist the dictionary of users and call the code in your sample multiple times you should consider that a user account is either new, modifed, or deleted.

The algorithm to use is the following when executing your SQL query:

  1. If row in query result is not in dictionary create and add new user to the dictionary.
  2. If row in query result is in dictionary update the user information.
  3. If dictionary item not in query result delete the user from the dictionary.

I'd also recommend not using SELECT *
Use only the table columns your code needs, this improves the performance of your code, and prevents a potential security breach by returning private user information.

浪荡不羁 2024-11-16 05:29:09

我不知道为什么你不在你的sql中使用distinct子句来获取唯一的结果。也会更快。你看过使用哈希表吗?

i am not sure why are you not using distinct clause in your sql to fetch unique results. also that will be faster. did you look at using hashtables.

剪不断理还乱 2024-11-16 05:29:09

我会将用户放入字典中并检查 allready 是否存在,如下所示:

Dictionary<int, Users> allUsers = new Dictionary<int, Users>()

然后在 Reader while 循环中:

int userId = int.Parse(reader["Id"].ToString());
Users currUser = allUsers[userId];

if (currUser == null)
{
  currUser = new Users();
  currUser.userId = userId);
  currUser.firstName = reader["accFirstName"].ToString();
  currUser.lastName = reader["accLastName"].ToString();
  allUsers.Add(userID, currUser);
}

Websites domainData = new Websites();
domainData.domainName = reader["accDomainName"].ToString();
domainData.userName = reader["accUserName"].ToString();
domainData.password = reader["accPass"].ToString();
domainData.URL = reader["accDomain"].ToString();
currUser.DomainData.Add(domainData);

I would put users into Dictonary and check if allready exists, something like this :

Dictionary<int, Users> allUsers = new Dictionary<int, Users>()

and then in Reader while loop :

int userId = int.Parse(reader["Id"].ToString());
Users currUser = allUsers[userId];

if (currUser == null)
{
  currUser = new Users();
  currUser.userId = userId);
  currUser.firstName = reader["accFirstName"].ToString();
  currUser.lastName = reader["accLastName"].ToString();
  allUsers.Add(userID, currUser);
}

Websites domainData = new Websites();
domainData.domainName = reader["accDomainName"].ToString();
domainData.userName = reader["accUserName"].ToString();
domainData.password = reader["accPass"].ToString();
domainData.URL = reader["accDomain"].ToString();
currUser.DomainData.Add(domainData);
戏舞 2024-11-16 05:29:09

看来问题的根源在于数据库表中。
当您说重复的数据行时,您是说列表中出现重复的条目还是表中存在重复的数据?

给出 2 行重复的行。

两种选择:
首先,通过使用不同的子句来防止从 sql 中提取重复数据,例如:
选择与安东尼奥提到的第二个选项不同的地方

是检查列表是否已经有它。

除非有其他原因,否则建议选择第一个选项。

Seems like the root of your problem is in your database table.
When you said duplicate data rows, are you saying you get duplicate entries in the list or you have duplicate data in your table?

Give 2 rows that are duplicate.

Two options:
First, prevent pulling duplicate data from sql by using a distinct clause like:
select distinct from where

Second option as mentioned Antonio, is to check if the list already has it.

First option is recommended unless there are other reasons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文