Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从sqldatasource删除重复项_C#_Sql Server_Duplicates_Sqldatasource - Fatal编程技术网

C# 从sqldatasource删除重复项

C# 从sqldatasource删除重复项,c#,sql-server,duplicates,sqldatasource,C#,Sql Server,Duplicates,Sqldatasource,我正在尝试创建一个“联系我们”页面,我在该页面上有一个下拉列表,使用SqlDataSource从我的数据库中提取列表,但我的数据库有重复的条目,我不想在列表中显示,是否仍可以删除重复的条目?我一辈子都想不出来 这是我的密码,谢谢 <%@ Page Language="C#" %> <!DOCTYPE html> <script runat="server"> protected void Page_Load(object sender, EventArg

我正在尝试创建一个“联系我们”页面,我在该页面上有一个下拉列表,使用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>

受保护的无效页面加载(对象发送方、事件参数e)
{
DropDownList2.Visible=false;
DropDownList3.Visible=false;
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
int ID=DropDownList1.SelectedIndex+1;
字符串_courseID=ID.ToString();
StringCollection idCollection=新建StringCollection();
SqlDataSource2.SelectCommand=“从TB_章节中选择*,其中c_CourseID=”+_CourseID+”;
DropDownList2.DataBind();
DropDownList2.Visible=true;
DropDownList3.Visible=false;
}



从TB_章节中选择DISTINCT*,其中c_CourseID=“+”CourseID+”
使用
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