如何循环通过字典

时间:2012-05-04 17:53:26

标签: windows

使用VS2010构建窗口表单应用程序。在我的登录表单中我收集了useid
和密码,然后点击登录按钮,如果验证成功直接
用户到主表单。
我想使用Dictionary来存储从DB读取的用户ID和密码 然后关闭连接。然后我比较文本框中的输入值和值:
用户并在字典中传递。成功直接到主要形式
这是我的代码。请帮忙。

string connectionstring =
   "Data Source =localhost;Initial Catalog=HSM;" +
   "User Id=sysad;Password=mypassword";

   SqlConnection connection = new SqlConnection(connectionstring);
   SqlCommand selectcmd = new SqlCommand("Select * from users");
   SqlDataReader reader ;
   Dictionary<string,string> logintest = new Dictionary<string,string>
   try
   {
      connection.Open();
      reader = selectcmd.ExecuteReader();
      while (reader.Read())
      {                
         Mainform main1 = new Mainform();
         this.Hide();
         main1.Show();
      }

      reader.Close();
       `  

1 个答案:

答案 0 :(得分:0)

简单

foreach (KeyValuePair<string, int> pair in logintest)
{
  Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}