从sqldatasource中删除重复项

时间:2013-08-16 14:01:02

标签: c# sql-server duplicates sqldatasource

我正在尝试创建一个“联系我们”页面,我在此页面上有一个下拉列表,使用SqlDataSource从我的数据库中提取列表,但我的数据库有重复的托管,我不希望在列表中显示,无论如何我可以删除重复项吗?我不能为我的生活弄明白。

这是我的当前代码,谢谢。

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList2.Visible = false;
    DropDownList3.Visible = false;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int ID = DropDownList1.SelectedIndex +1;
    string _courseID = ID.ToString();
    StringCollection idCollection = new StringCollection();
    SqlDataSource2.SelectCommand = "SELECT * FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' ";
    DropDownList2.DataBind();
    DropDownList2.Visible = true;
    DropDownList3.Visible = false;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="m_ModuleName" DataValueField="m_ID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_modules]"></asp:SqlDataSource>
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="c_ChapterName" DataValueField="c_ChapterID"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_Chapter] WHERE ([c_CourseID] = @c_CourseID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="c_CourseID" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="c_ChapterTopicName" DataValueField="c_ChapterTopicID"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:OpenIT_DBConnectionString3 %>" SelectCommand="SELECT * FROM [TB_Chapter] WHERE ([c_ChapterID] = @c_ChapterID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList2" Name="c_ChapterID" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
</div>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT * FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' "

答案 1 :(得分:0)

使用DISTINCT关键字。


WITH UniqueChapterxxx AS
    (
        SELECT m_ID, m_ID,xxx,
            ROW_NUMBER() OVER(PARTITION BY m_ID ORDER BY m_ID) AS 'RowNum'
        FROM TB_Chapter WHERE c_CourseID = '" + _courseID + "' "
    )
    SELECT *
    FROM UniqueChapterxxx
    WHERE RowNum = 1

Check This Link