如何从垂直存储的sql table中赋值?

How to assign values from sql table which is stored vertically?

我一直在检查许多其他类似的问题,但找不到答案。我有一种情况需要显示 UI 一个网格,其值将从垂直数据 table.

中提取

例如,在 UI 中,我必须根据来自数据库产品 table

的 P_ID 显示如下所示的网格
Product name IsLaunched Exp_Price
AC1 1 4000
AC2 0 3000
AC3 0 3000

网格中的值将从数据库中填充,该数据库存储在产品 table 中,如下所示:

P_ID Product name IsLaunched Exp_Price
1 LCD TV 1 2000
1 LED TV 1 2000
2 AC1 1 4000
2 AC2 0 3000
2 AC3 0 3000

所以如果我从下拉列表中选择 select 2,结果网格应该显示所有具有 p_id 的产品作为 2.

使用您的示例数据生成的示例查询

select P_ID, `Product name`, IsLaunched, Exp_Price
from product
where P_ID = 2

到目前为止,还不错。现在,我们需要确保我们动态接收下拉列表中设置的值。这意味着在 UI 上发生了一些改变值的事件。如果您正在开发 Web 应用程序,那么这可能是您的服务器端需要处理的请求参数。如果是桌面应用程序,则应用程序的后端需要处理该事件。无论如何,根据您使用的应用程序堆栈,您将需要处理该值,构建一个参数化查询,在其中将所选 ID(并防止 SQL 注入恶作剧)传递给查询执行程序,最终 运行 查询和 return 结果。

您可以通过 P_ID:

过滤来做到这一点
  private void LoadData(string P_ID_Selected)
    {
        try
        {
            //P_ID_Selected parameter send from drop-down selectedIndexChanged event
            string ConnStringDB = "your connection string here";
            string query = "SELECT P_ID, `Product name`, IsLaunched, Exp_Price from product where P_ID = " + P_ID_Selected;
            SqlConnection con = new SqlConnection(ConnStringDB);//connection name
            con.Open();
            SqlCommand cmd = new SqlCommand(query, con)
            {
                CommandType = CommandType.Text
            };
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "DataSetName");
            GridViewName.DataSource = ds.Tables["DataSetName"];
            con.Close();
        }
        catch
        {
            GridViewName.DataSource = null;
        }
        finally
        {
            GridViewName.DataBind();
        }
    }

这是我实施的解决方案,我觉得它可能对其他有类似查询的人有用。

在 .aspx 页面中

<table id="tblid" style="width: 100%; text-align: center;" runat="server">
  <tr>
    <td align="center">
      <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
    </td>
  </tr>
</table>

在aspx.cs页

  private void loadProductTable()
    {
        try
        {

            string ProductID =  **id from ui** 
            DataTable dt = GetProductDetails(ProductID);

            StringBuilder htmlTable = new StringBuilder();
            htmlTable.Append("<!DOCTYPE>");
            htmlTable.Append("<table border='1'");
            htmlTable.Append("style ='font-size: 12px; border-spacing: 0px; letter-spacing: 0.48px; font-family: Arial; line-height: 25px; text-align: center; margin : 1%;'>");
            htmlTable.Append("<tr style='background-color:grey; color: White;'>");
            htmlTable.Append("<th style='width : 350px'> Product Name </th>");
            htmlTable.Append("<th style='width : 100px'> Is Lanched</th>");
            htmlTable.Append("<th style='width : 100px'>Expected Price (In USD) </th>");               

            if (dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    htmlTable.Append("<tr>");
                    htmlTable.Append("<td style='text-align: left; padding-left: 10px;'>" + dt.Rows[i]["Product_name"] + "</td>");
                    htmlTable.Append("<td>" + dt.Rows[i]["IsLaunched"] + "</td>");
                    htmlTable.Append("<td>" + dt.Rows[i]["Exp_Price"] + "</td>");
                    htmlTable.Append("</tr>");
                }
                htmlTable.Append("</table>");
                DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });                    
                
            }
            else
            {
                htmlTable.Append("<tr>");
                htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                htmlTable.Append("</tr>");
            }
        }
        catch (SystemException ex)
        {
            ExceptionManager.HandleException(ex);
        }

    }

和存储过程

CREATE PROCEDURE GetProductDetails
    (
     @ProductID int 
    )
AS                      
BEGIN                      
SET NOCOUNT ON;    
    
BEGIN try     
      
DECLARE @ErrorMessage  VARCHAR(4000),    
              @ErrorSeverity INT,    
              @ErrorState    INT,    
              @Error         VARCHAR(40)  

select  ROW_NUMBER() OVER
      (
        ORDER BY [PrimaryKeyID] ASC     
      ) 
      AS RowNumber,
      P_ID,
      Product_name,
      CASE WHEN IsLaunched= 0 THEN 'No'
      ELSE 'Yes'
      END AS IsLaunched,
      Exp_Price INTO #temp
      FROM product 
      WHERE p_id = @ProductID     
      
      SELECT  * FROM #temp WHERE RowNumber <> 1 // since i dont want to show the first default product


              
END TRY

---------Begin Catch-------------------------     
  BEGIN catch    
                   SELECT @ErrorMessage = Error_message(),     
                    @ErrorSeverity = Error_severity(),     
                    @Error               =@@Error,     
                    @ErrorState = Error_state();    
    
          RAISERROR ( @ErrorMessage,@ErrorSeverity,@ErrorState );     
    
           
    
END catch    
RETURN 1    
    
SET NOCOUNT OFF;    
    
End