我使用asp.net C#(库存管理项目)在网站上工作
我有这个数据库:
CREATE TABLE [dbo].[product] (
[category_id] INT IDENTITY (1, 1) NOT NULL,
[company_na] VARCHAR (50) NOT NULL,
[category] VARCHAR (MAX) NOT NULL,
[items] VARCHAR (MAX) NOT NULL,
[quantity] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([category_id] ASC)
);
我使用此代码显示图表中每个项目的数量百分比类别:
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
cmd.Connection = con;
cmd.CommandText = "select category ,quantity from product where company_na = '" + Label5.Text + "'";
con.Open();
da.SelectCommand = cmd;
da.Fill(dt);
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
Chart1.Series[0].Points.DataBindXY(x, y);
Chart1.Series[0].IsValueShownAsLabel = true;
foreach (DataPoint p in Chart1.Series[0].Points)
{
p.Label = "#PERCENT";
}
但是,它显示每个项目的重复类别。 我希望所有类别都没有重复。
答案 0 :(得分:0)
您的查询中可能需要SELECT DISTINCT
cmd.CommandText = "select distinct category_id ,quantity from product where company_na = '" + Label5.Text + "'";
答案 1 :(得分:0)
选择distinct将不起作用Trisno,问题是Moon Light有多个他需要来自db的字段,我们并不真正知道另一个字段的行为。无论如何,这就是我要做的事情:
cmd.CommandText =
"SELECT c2.field1 ,
field2
FROM (SELECT DISTINCT
field1
FROM dbo.TABLE AS C
) AS c1
JOIN dbo.TABLE AS c2 ON c1.field1 = c2.field1'" + Label5.Text + "'";
请告诉我这是否有帮助;)