2016-07-26 4 views
0

SQLExpressサーバーに接続していて、テーブルからデータを返そうとしています。コードは接続を行っていますが、クエリから結果を読み取ったときにはデータはありません。私はSSMSでクエリを実行し、それは正常に動作します。私はまた別のアプリケーションで同じコードを使用し、それは正常に動作します。私は今混乱している。ここに私の接続は日常的である:ここではSQLDataReaderがデータを返さない

private void ConnectToDatabase() 
    { 
     string strConnection = null; 


     try 
     { 
      if (sqlConn != null) 
      { 
       sqlConn.Close(); 
      } 


      strConnection = "Data Source=CASS-LAPTOP\\SQLEXPRESS; Initial Catalog=NBFoodPantry;Integrated Security=true"; 

      sqlConn = new SqlConnection(@strConnection); 
      try 
      { 
       sqlConn.Open(); 

      } 
      catch (Exception ex) 
      { 
       string strMsg; 

       strMsg = "ConnectToDatabase: SQL Open failed with error, " + ex.Message + "."; 
       System.Windows.MessageBox.Show(strMsg); 
      } 
     } 
     catch (Exception ex) 
     { 
      string strMsg; 

      strMsg =" ConnectToDatabase: failed with error, " + ex.Message + "."; 
      System.Windows.MessageBox.Show(strMsg); 
     } 
    } 

は、テーブルを照会するためのコードは次のとおりです。

private void LoadCitys() 
    { 
     bool blnSuccess = false; 
     int intItemCnt; 
     string strQuery; 


     if (sqlConn != null && sqlConn.State == ConnectionState.Open) 
     { 
      intItemCnt = 0; 
      strQuery = "select distinct city from zipcodes order by city"; 

      try 
      { 
       using (SqlCommand sqlCmd = new SqlCommand(strQuery, sqlConn)) 
       { 
        SqlDataReader sqlDataRead = sqlCmd.ExecuteReader(); 

        while (sqlDataRead.Read()) 
        { 
         string strDBNme = sqlDataRead.GetString(intItemCnt); 
         cmbxACCity.Items.Add(strDBNme); 
        } 

        sqlDataRead.Close(); 
        sqlCmd.Dispose(); 
        cmbxACCity.SelectedItem = cmbxACCity.Items.GetItemAt(0); 
       } 

       blnSuccess = true; 
      } 
      catch (Exception exQuery) 
      { 
       System.Windows.MessageBox.Show("LoadCitys: Error, " + exQuery.Message + ", has occurred."); 
       blnSuccess = false; 
      } 
     } 
    } 
+0

例外はありますか? –

+0

デバッグ時の距離はどれくらいですか? whileループに入りますか? 'SqlConnection' ctorの" @strConnection "の構文は何ですか? – dlatikay

+0

私は今混乱しています。例外がスローされたかどうかを再度チェックしてそのメッセージを取得し、それが動作していないかどうかを確認するために、アプリケーションを再度実行しました。なぜ私は考えていない。助けてくれてありがとう。 – Cass

答えて

0

私は何が起こったのかわからないが、例外がいた場合、私はちょうどダブルチェックに再度アプリケーションを実行しましたスローされ、そのメッセージを取得し、それは動作していません。なぜ私は考えていない。助けてくれてありがとう。

関連する問題