Retrieving values from SQL Server
My table in SQL Server has 3 fields
serial no. (int)
days (varchar)
status (bit)
With the help of a dictionary how can I retrieve days and status as a 开发者_StackOverflow中文版key-value pair from SQL Server using a SqlDataReader
?
This seems very much like homework, so here's some pseudocode to help you towards the answer:
- Create a Dictionary to hold the values you retrieve.
- Open a database connection.
- Create a SqlCommand that selects the relevant values out of the table
- Create a SqlDataReader using the SqlCommand created previously.
- Iterate over the reader using its .Read method.
- Get the Day field and Status field and put them in the dictionary
- Repeat step 1 until .Read returns false
- You're done!
I've put a large number of links to pages on msdn.microsoft.com into the steps I've detailed. 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>;
精彩评论