简体   繁体   English

从SQL Server检索值

[英]Retrieving values from SQL Server

My table in SQL Server has 3 fields 我在SQL Server中的表有3个字段

serial no. (int)
days       (varchar)
status     (bit)

With the help of a dictionary how can I retrieve days and status as a key-value pair from SQL Server using a SqlDataReader ? 借助字典,如何使用SqlDataReader从SQL Server中检索天和状态作为键值对?

This seems very much like homework, so here's some pseudocode to help you towards the answer: 这似乎很像家庭作业,因此这里有一些伪代码可以帮助您找到答案:

  1. Create a Dictionary to hold the values you retrieve. 创建一个词典来保存您检索的值。
  2. Open a database connection . 打开数据库连接
  3. Create a SqlCommand that selects the relevant values out of the table 创建一个SqlCommand ,从表中选择相关值
  4. Create a SqlDataReader using the SqlCommand created previously. 使用先前创建的SqlCommand创建SqlDataReader
  5. Iterate over the reader using its .Read method. 使用其.Read方法遍历阅读器。
    1. Get the Day field and Status field and put them in the dictionary 获取“ 日期”字段和“ 状态”字段并将其放入字典中
    2. Repeat step 1 until .Read returns false 重复步骤1,直到.Read返回false
  6. You're done! 你完成了!

I've put a large number of links to pages on msdn.microsoft.com into the steps I've detailed. 我已将许多指向msdn.microsoft.com上页面的链接放入我已详细介绍的步骤中。 By reading through those, and looking at the code samples that are present on each page, you should be able to put together some working code =) 通过阅读这些内容,并查看每一页上的代码示例,您应该能够将一些有效的代码放在一起=)

Dictionary<int, Dictinary<string,bool> dict=new Dictionary<int, Dictinary<string,bool>;

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

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