0

我希望我的线程的标题是有意义的。

我们正在构建一个汽车经销商网络应用程序,允许潜在的汽车购买者确定哪种类型的汽车适合他们的预算和需求。

因此,我们有汽车制造商、汽车型号、汽车状况、里程、价格范围等的下拉列表。

下面的代码显示了汽车品牌的下拉列表。

当用户选择他/她喜欢的汽车品牌时,与该品牌相关的模型会自动加载到第二个下拉列表中。

    <form id="form1" runat="server">
    <div>
        <span style ="font-family:Arial">Select Make : </span> 
        <asp:DropDownList ID="ddlMake" runat="server" AutoPostBack = "true" OnSelectedIndexChanged="ddlMake_SelectedIndexChanged">
        <asp:ListItem Text = "------" Value = ""></asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <span style ="font-family:Arial">Select Model : </span> 
        <asp:DropDownList ID="ddlModel" runat="server" Enabled = "false" AutoPostBack = "true" OnSelectedIndexChanged="ddlModel_SelectedIndexChanged">
        <asp:ListItem Text = "------" Value = ""></asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <asp:button id="btnSave" runat="server" Text="Submit New Request"></asp:button><br /><br />
        <asp:Label ID="lblResults" runat="server" Text="" Font-Names = "Arial"></asp:Label>        
    </div>
    </form>



     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Not IsPostBack Then
                ddlMake.AppendDataBoundItems = True
                Dim strConnString As [String] = ConfigurationManager _
                 .ConnectionStrings("amsconstr").ConnectionString
                Dim strQuery As [String] = "select vehicleId, Make from MakeModel"
                Dim con As New SqlConnection(strConnString)
                Dim cmd As New SqlCommand()
                cmd.CommandType = CommandType.Text
                cmd.CommandText = strQuery
                cmd.Connection = con
                Try
                    con.Open()
                    ddlMake.DataSource = cmd.ExecuteReader()
                    ddlMake.DataTextField = "Make"
                    ddlMake.DataValueField = "vehicleId"
                    ddlMake.DataBind()
                Catch ex As Exception
                    Throw ex
                Finally
                    con.Close()
                    con.Dispose()
                End Try
            End If
        End Sub
   Protected Sub ddlMake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            ddlModel.Items.Clear()
            ddlModel.Items.Add(New ListItem("--Select Model--", ""))

            ddlModel.AppendDataBoundItems = True
            Dim strConnString As [String] = ConfigurationManager _
                       .ConnectionStrings("amsconstr").ConnectionString
            Dim strQuery As [String] = "select vehicleId Employee from MakeModel " _
                                        & "where vehicleId=@vehicleId"
            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand()
            cmd.Parameters.AddWithValue("@vehicleId", _
                                 ddlMake.SelectedItem.Value)
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con
            Try
                con.Open()
                ddlModel.DataSource = cmd.ExecuteReader()
                ddlModel.DataTextField = "Model"
                ddlModel.DataValueField = "VehicleId"
                ddlModel.DataBind()
                If ddlModel.Items.Count > 1 Then
                    ddlModel.Enabled = True
                Else
                    ddlModel.Enabled = False
                End If
            Catch ex As Exception
                Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
        End Sub
  Protected Sub ddlModel_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

            Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
            Dim strQuery As [String] = "select VehicleId, Model from MakeModel where VehicleId=@vehicleId"
            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand()
            cmd.Parameters.AddWithValue("@vehicleId", ddlModel.SelectedItem.Value)
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con
            Try
                con.Open()
            Catch ex As Exception
                Throw ex
            Finally

                con.Close()
                con.Dispose()
            End Try
    End Sub

这工作正常。

我们想为每个品牌的汽车附加可用记录的数量。

例如,在 Make of a car 下拉列表中,我们有如下内容:

马自达奔驰丰田等

我们希望用户知道特定品牌如何可用,例如:

Mazda (10)
Mercedez Benz (2)
Toyota (7)

如何为特定品牌的汽车添加可用汽车数量?

4

2 回答 2

1

您需要使用 CONVERT() 函数。

select VehicleId, (Model + '(' + CONVERT(NVARCHAR, (select count(*) from MakeModel B where a.ID=b.ID)) + ')') as Make from MakeModel A
于 2013-08-30T15:27:25.153 回答
0

更改您的 SQL 语句,使其返回您需要的所有内容。而不仅仅是:

select VehicleId, Model from MakeModel.....

使用类似的东西:

select VehicleId, (Model + '(' + (select count(*) from MakeModel B where a.ID=b.ID) + ')') as Make from MakeModel A...

..这不太好,但会起作用。使用 SQL Server CTE 使代码更好。或者甚至更好地将最终代码放入存储过程并调用此过程。

还有一个注意事项 - 尽量减少重复代码。希望能帮助到你。

于 2013-08-30T14:19:43.170 回答