2017-06-06 22 views
0

當我嘗試在SqlDataReader的數據庫中僅顯示不同的值時,它仍顯示重複的列。顯示類似字段的SqlDataReader

string State = ddlState.SelectedItem.Value; 
string City = ddlCities.SelectedItem.Text; 

SqlConnection con = new SqlConnection("Data Source=--myDB--;Database=--MyconnectionString--"); 

SqlCommand cmd = new SqlCommand("Select distinct State, City, ExchangeType, Boid, Bex, Npa, Nxx from TAS where StateId=" + "'" + State + "'" + " AND City=" + "'" + City + "'", con); 

if (!string.IsNullOrEmpty(ddlCities.SelectedItem.Text)) 
{ 
    exchangesheet.Visible = true; 

    con.Open(); 
    SqlDataReader rdr = cmd.ExecuteReader(); 

    while (rdr.Read()) 
    { 
     this.STATE.Text = rdr["State"].ToString(); 
     this.CITY.Text = rdr["City"].ToString(); 
     this.ExchangeType.Text = rdr["ExchangeType"].ToString(); 
     this.BOID.Text += rdr["Boid"].ToString() + ", "; 
     this.BEX.Text += rdr["Bex"].ToString() + ", "; 
     this.NPA.Text += rdr["Npa"].ToString() + ", "; 
     this.NXX.Text += rdr["Nxx"].ToString() + ", "; 
    } 

    con.Close(); 
} 
else 
{ 
    exchangesheet.Visible = false; 
} 

+=所有查詢導致標籤重複數據,即使我只指定不同的數據在SqlCommand查詢中顯示。如果我沒有正確解釋,我很抱歉,但任何幫助或方向將不勝感激。搜索特定狀態時

結果:

BOID: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 
BEX: 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205, 9205,  
NPA: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205,  
NXX: 212, 214, 226, 238, 244, 250, 251, 252, 254, 262, 263, 278, 279, 290, 298, 307, 313, 314, 320, 321, 322, 323, 324, 326, 328, 380, 397, 402, 403, 408, 414, 421, 423, 437, 444, 445, 453, 458, 502, 520, 533, 536, 545, 558, 583, 591, 592, 595, 599, 682, 714, 731, 733, 776, 780, 781, 783, 786, 787, 788, 791, 798, 801, 802, 803, 808, 822, 823, 824, 833, 836, 841, 854, 856, 868, 870, 871, 873, 876, 879, 913, 916, 917, 923, 925, 929, 930, 933, 939, 940, 941, 942, 943, 945, 949, 951, 956, 957, 967, 968, 969, 970, 972, 977, 978, 979, 980, 981, 982, 983, 985, 987, 988, 989, 991, 995, 
+0

你試圖添加你的SQL SELECT語句中有一個GROUP BY子句? – abhi

+0

[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不**連接你的SQL語句 - 使用**參數化查詢**,而不是避免SQL注入 - 檢查[小Bobby表](https://xkcd.com/327/) –

+0

我還沒有嘗試過GROUP BY子句,我會研究這一點。 這是我在工作中使用的一個小型內部工具,所以我現在不擔心注射,但謝謝! – jsnhndrsn1985

回答

1

當使用distinct創建查詢時,它將爲您提供所有選定列的獨特組合。完成你想要的東西,它會更喜歡這個

SqlCommand cmd = new SqlCommand("Select distinct State from TAS where StateId=" + "'" + State + "'" + " AND City=" + "'" + City + "'", con); 
...code to collate all the unique values and add to appropriate label... 

SqlCommand cmd = new SqlCommand("Select distinct City from TAS where StateId=" + "'" + State + "'" + " AND City=" + "'" + City + "'", con); 
...code to collate all the unique values and add to appropriate label... 

SqlCommand cmd = new SqlCommand("Select distinct Exchange from TAS where StateId=" + "'" + State + "'" + " AND City=" + "'" + City + "'", con); 
...code to collate all the unique values and add to appropriate label... 

還有許多其他的方式來完成同樣的事情,以及

對於重複多個查詢,你可以使用的東西大致是這樣的

SqlCommand cmd = new SqlCommand..the query for getting unique state 
con.Open(); 
using (SqlDataReader rdr = cmd.ExecuteReader()) 
    { 
    while (rdr.Read()) 
     { 
      this.STATE.Text = rdr["State"].ToString(); 
     } 
    } 

然後重複別人

+0

我會試一試。謝謝! – jsnhndrsn1985

+0

看來它不像我打開多個SqlDataReader reader = cmd.ExecuteReader();同時運行。我已經分離了SqlCommands並更新了變量,但告訴我原始閱讀器需要關閉,不過簡單地說明rdr.Close();也不起作用。 – jsnhndrsn1985

+0

像這樣做 SqlCommand cmd = new SqlCommand ..獲取唯一狀態的查詢 con.Open();使用(SqlDataReader rdr = cmd.ExecuteReader()) { while(rdr.Read()) {this.STATE.Text = rdr [「State」]。ToString(); } } 然後重複說明下一個城市,交換類型等 –

0

什麼,當你更換+ = =用會怎麼樣?

+0

它將只顯示每個字段的值,並不顯示任何其他唯一值, \t BOID:205,\t BEX:9205,\t NPA:205,\t NXX:995, – jsnhndrsn1985

0

假設您出於某種原因無法對數據庫進行多重查詢,並且結果數據集很小。如果你的服務器往返時間很長,或者你的DBA會討厭你用6個表掃描來發送垃圾郵件,這可能會更快。 你可以在你的代碼中執行實際的區別。

 con.Open(); 
     var hsNxx = new HashSet<int>(); 
     var hsBex = new HashSet<int>(); 
     using (SqlDataReader rdr = cmd.ExecuteReader()) 
     { 


      while (rdr.Read()) 
      { 
       hsBex.Add((int)rdr["Bex"]); 
       hsNxx.Add((int)rdr["Nxx"]); 

      } 

     } 

     con.Close(); 
     string distinctBex =hsBex.Count == 1 ? hsBex.First().ToString() : hsBex.Select(f => f.ToString()).Aggregate((x, y) => x + "," + y); 
     this.BEX.Text = distinctBex; 

假定列是整數。 你也可以實現文本僅更改一次,而不是突變的每一行與+ =

作爲一個方面說明我建議使用子句中包紮連接/讀卡器/命令IDisposables