SqlDataReader显示类似字段

时间:2017-06-06 16:31:09

标签: c# asp.net 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,

3 个答案:

答案 0 :(得分: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();
       }
    }

然后重复其他

答案 1 :(得分:0)

将+ =替换为??

时会发生什么

答案 2 :(得分: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;

假设列是整数。 您还可以将文本实现仅更改一次,而不是每行都使用+ =

进行变更

作为旁注,我建议在using子句中使用连接/阅读器/命令的IDisposables