选择事务,仅显示一行但在数据库中有多行

时间:2015-10-01 05:47:41

标签: c# asp.net

在这个空白中,我选择了交易。例如,数据库中有3行,其PON为17,我想在列表视图中只显示一行。有可能吗?

void GetSupply()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Supplier.SupplierName, PurchaseOrder.PONo, PurchaseOrder.PODate, " +
        "PurchaseOrder.PODateReceived, PurchaseOrder.PODeliveryDate, PurchaseOrder.POPaymentMethod, " +
        "PurchaseOrder.POReceiptNo, PurchaseOrderDetails.POAmount, PurchaseOrder.POPaymentDate, " +
        "PurchaseOrder.POStatus FROM PurchaseOrder INNER JOIN PurchaseOrderDetails " +
        "ON PurchaseOrder.PONo = PurchaseOrderDetails.PONo INNER JOIN Supplier ON " +
        "PurchaseOrder.SupplierNo = Supplier.SupplierID " +
        "ORDER BY PurchaseOrder.PODate DESC";
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "PurchaseOrderDetails");
    lvPO.DataSource = ds;
    lvPO.DataBind();
    con.Close();
}

现在看起来像这样

enter image description here

我想像这样展示

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以尝试使用select DISTINCT,即

cmd.CommandText = "SELECT DISTINCT Supplier.SupplierName, PurchaseOrder.PONo, PurchaseOrder.PODate, " +
        "PurchaseOrder.PODateReceived, PurchaseOrder.PODeliveryDate, " +
        "FROM PurchaseOrder INNER JOIN PurchaseOrderDetails " +
        "ON PurchaseOrder.PONo = PurchaseOrderDetails.PONo INNER JOIN Supplier ON " +
        "PurchaseOrder.SupplierNo = Supplier.SupplierID " +
        "ORDER BY PurchaseOrder.PODate DESC";

或尝试使用GROUP BY

cmd.CommandText = "SELECT Supplier.SupplierName, PurchaseOrder.PONo, PurchaseOrder.PODate, " +
        "PurchaseOrder.PODateReceived, PurchaseOrder.PODeliveryDate, " +
        "FROM PurchaseOrder INNER JOIN PurchaseOrderDetails " +
        "ON PurchaseOrder.PONo = PurchaseOrderDetails.PONo INNER JOIN Supplier ON " +
        "PurchaseOrder.SupplierNo = Supplier.SupplierID " +
        "GROUP BY PurchaseOrder.PONo,Supplier.SupplierName, PurchaseOrder.PODateReceived, PurchaseOrder.PODeliveryDate ORDER BY PurchaseOrder.PODate DESC";