无法加入 ASP.NET 中的表

Can't JOIN tables in ASP.NET

我需要根据所选的子类别获取产品。现在,它显示所有产品。这该怎么做?这是我的代码。如何在按钮点击中传递Subcategory.Id?

...
<td>Subcategory</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Id">
    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource2"
         runat="server"
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
         SelectCommand="SELECT [Id], [Name] FROM [SubCategory] WHERE ([IdCategory] = @IdCategory)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="IdCategory" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

</td>
</tr>
<tr>
<td>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
</tr>
...



protected void Button1_Click(object sender, EventArgs e)
{
...
        SqlCommand command = new SqlCommand("SELECT productName, quantity, price FROM Product JOIN SubCategory ON Product.id_subcategory = SubCategory.id", _connection);
...

}

假设您有以下数据库 table 结构:

Product
- Id (PK)
- ProductName
- Quantity
- Price
- MainCatID (FK)
- SubCatID (FK)
- others...

Category
- Id (PK)
- Name

SubCategory
- Id (PK)
- Name
- IdCategory (FK)
  1. 确保添加命名空间 System.Configuration 以便您可以从 web.config 文件访问连接字符串。
  2. 实例化一个 SqlConnection class 来识别数据库连接:
  3. 创建方法以从各自的下拉列表控件中显示类别和子类别的列表
  4. 在 DropDownList1 中包含一个 AutoPostBack 属性 然后设置为 true 这样每次你从列表中 select 一个项目时,它将 'regenerate' 一个子列表基于 selected 类别值的类别。
  5. 调用 DisplaySubCategories() 方法创建 DropDownList1 的 OnSelectedIndexChanged 事件。
  6. 创建一个方法来显示基于 selected 类别和子类别值的产品列表
  7. 包括从步骤 #4 到 DropDownList2 的类似过程
  8. 在页面加载事件中调用这两个方法。

SqlConnection con = new SqlConnection(ConfigurationManager.
    ConnectionStrings["ConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DisplayCategories();
        DisplaySubCategories();
    }
}

void DisplayCategories()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Id, Name FROM Category";
    SqlDataReader data = cmd.ExecuteReader();
    DropDownList1.DataSource = data;
    DropDownList1.DataTextField = "Name";
    DropDownList1.DataValueField = "Id";
    DropDownList1.DataBind();
    con.Close();
}

void DisplaySubCategories(string ID)
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Id, Name FROM SubCategory WHERE IdCategory = @IdCategory";
    cmd.Parameters.AddWithValue("@IdCategory", ID);
    SqlDataReader data = cmd.ExecuteReader();
    DropDownList2.DataSource = data;
    DropDownList2.DataTextField = "Name";
    DropDownList2.DataValueField = "Id";
    DropDownList2.DataBind();
    con.Close();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DisplaySubCategories(DropDownList2.SelectedValue);
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    DisplayProducts(DropDownList1.SelectedValue, DropDownList2.SelectedValue);
}

void DisplayProducts(string mainCatID, string subCatID)
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = @"SELECT productName, quantity, price FROM Product 
        WHERE MainCatID = @MainCatID AND SubCatID=@SubCatID";
    cmd.Parameters.AddWithValue("@MainCatID", mainCatID);
    cmd.Parameters.AddWithValue("@SubCatID", subCatID);
    SqlDataReader data = cmd.ExecuteReader();
    string result = string.Empty;
    while (data.Read())
    { 
        result += "Name = " + Convert.ToString(reader["productName"]) + "; ";
        result += "Quantity = " + Convert.ToString(reader["quantity"]) + "; ";
        result += "Price = " + Convert.ToString(reader["price"]);
        result += "<br />";
    }
    ReadAllOutput.Text = result;
    con.Close();
}

.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" />

我删除了所有现有的 SqlDataSource 控件,因为我发现它们很乱。

更新:

您还可以在按钮点击事件中声明 DisplayProducts() 方法

protected void Button1_Click(object sender, EventArgs e)
{
    DisplayProducts(DropDownList1.SelectedValue, DropDownList2.SelectedValue);
}

PS:某些语法可能不正确(区分大小写),目前我没有使用 IDE。

我假设 DropDownList2 控件已经包含子类别数据。

您可以在 Button1_Click 事件中通过

获取您的子类别 ID
DropDownList2.SelectedValue