2017-03-05 36 views
0

我有一個表:如何通過在C#列表下拉到存儲過程在SQLSERVER的價值

enter image description here

和乘以最後的結果如下:

enter image description here

一程序的定義如下:(必須將下拉列表的值與數據庫中的值相乘,然後將它們水平相加,並在最終結果中顯示resault。

CREATE PROCEDURE [dbo].[Cstored]  
AS 
BEGIN 
    SET NOCOUNT ON; 
DECLARE @Ta int 
DECLARE @Em int 
DECLARE @Ma int 
DECLARE @Sa int 

SET @Em = 8 >>>> need to be set by user selection in dropdows list 
SET @Ta= 6  >>>> need to be set by user selection in dropdows list 
SET @Sa= 5  >>>> need to be set by user selection in dropdows list 
SET @Ma = 7 >>>> need to be set by user selection in dropdows list 

SELECT 
    Namec, 
    Tar*@Ta AS val1, 
    Emk*@Em as val2, 
    Mas*@Ma as val3, 
    San*@Sa As val4, 
    (Tar*@Ta)+(Emk*@Em)+(Mas*@Ma)+(San*@Sa) as finalresult 
FROM Cdetail 
END 

正如你可以在屏幕截圖中看到的那樣,它在數據庫中運行良好,但是該值已在手動設置中。我希望它通過用戶選擇來設置。

SET @Em = 8 

它是數據庫,但我創建了ASP網頁一些下拉列表,以便用戶如下選擇值:

<form id="form1" runat="server"> 
     <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" > 
      <asp:ListItem Value="@Em">Emk</asp:ListItem> 
      <asp:ListItem Value="@Ta">Tar</asp:ListItem> 
      <asp:ListItem Value="@Sa">San</asp:ListItem> 
      <asp:ListItem Value="@Ma">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="@Em">Emk</asp:ListItem> 
      <asp:ListItem Value="@Ta">Tar</asp:ListItem> 
      <asp:ListItem Value="@Sa">San</asp:ListItem> 
      <asp:ListItem Value="@Ma">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="@Em">Emk</asp:ListItem> 
      <asp:ListItem Value="@Ta">Tar</asp:ListItem> 
      <asp:ListItem Value="@Sa">San</asp:ListItem> 
      <asp:ListItem Value="@Ma">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList4" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="@Em">Emk</asp:ListItem> 
      <asp:ListItem Value="@Ta">Tar</asp:ListItem> 
      <asp:ListItem Value="@Sa">San</asp:ListItem> 
      <asp:ListItem Value="@Ma">Mas</asp:ListItem> 
     </asp:DropDownList> 

每個下拉菜單需要有一個默認值如下:

DropDownList1.SelectedValue = 8; 
DropDownList2.SelectedValue = 7; 
DropDownList3.SelectedValue = 6; 
DropDownList4.SelectedValue = 5; 

這意味着下拉1中選擇的值必須發送到存儲過程的數字8(DD2數字7等等),然後設置變量然後乘以相關的v在數據庫中。 ,之後從上到下顯示gridviewfinalresult的值。

我試着用下面的代碼,但它不起作用,我不知道如何發送下拉值來存儲過程,並根據選擇提供每個項目。

protected void Button1_Click(object sender, EventArgs e) 
     { 
      DropDownList1.SelectedValue = 8; 
      DropDownList2.SelectedValue = 7; 
      DropDownList3.SelectedValue = 6; 
      DropDownList4.SelectedValue = 5; 

      SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["carConnectionString"]); 
      sqlCon.Open(); 
      SqlCommand scCommand = new SqlCommand("Carstored", sqlCon); 
      scCommand.CommandType = CommandType.StoredProcedure; 
      scCommand.Parameters.AddWithValue("????", DropDownList1); 
      scCommand.Parameters.AddWithValue("????", DropDownList2); 
      scCommand.Parameters.AddWithValue("????", DropDownList3); 
      scCommand.Parameters.AddWithValue("????", DropDownList4); 
      using (SqlDataAdapter sda = new SqlDataAdapter(scCommand)) 
      { 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 
       GridView1.DataSource = dt; 
       GridView1.DataBind(); 

我想,如果用戶在dropdow 1選擇Emk8數相乘(X)至所有Emk的值在表,並且如果用戶在dropdow 2選擇Tar7數相乘(X),將值表中的所有Tar

回答

0

您無需在存儲過程中聲明變量,聲明參數:

CREATE PROCEDURE [dbo].[Cstored]  
(
    @Ta int, 
    @Em int, 
    @Ma int, 
    @Sa int 
) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    SELECT 
    Namec, 
    Tar*@Ta AS val1, 
    Emk*@Em as val2, 
    Mas*@Ma as val3, 
    San*@Sa As val4, 
    (Tar*@Ta)+(Emk*@Em)+(Mas*@Ma)+(San*@Sa) as finalresult 
    FROM Cdetail 
END 

然後,在你的C#代碼,做這樣的事情:

DataTable dt; 
using(var sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["carConnectionString"])) 
{ 
    sqlCon.Open(); 
    using(var scCommand = new SqlCommand("Carstored", sqlCon)) 
    { 
     scCommand.CommandType = CommandType.StoredProcedure; 
     scCommand.Parameters.Add("@Ta", SqlDbType.Int).Value = DropDownList1.SelectedValue; 
     scCommand.Parameters.Add("@Em", SqlDbType.Int).Value = DropDownList2.SelectedValue; 
     scCommand.Parameters.Add("@Ma", SqlDbType.Int).Value = DropDownList3.SelectedValue; 
     scCommand.Parameters.Add("@Sa", SqlDbType.Int).Value = DropDownList4.SelectedValue; 
     using (SqlDataAdapter sda = new SqlDataAdapter(scCommand)) 
     { 
      dt = new DataTable(); 
      sda.Fill(dt); 
     } 
    } 
} 
+0

它不工作,有錯誤在StoredProcedure中,下拉列表沒有默認值(DropDownList1.SelectedValue = 8;等) – Mom

+0

什麼錯誤?您需要驗證用戶輸入... –

+0

in'sda.Fill(dt);'我收到此錯誤(無法將參數值從字符串轉換爲Int32), 此外,我找不到默認下拉值的定義((DropDownList1.SelectedValue = 8; DropDownList2.SelectedValue = 7; DropDownList3.SelectedValue = 6; DropDownList4.SelectedValue = 5;)) – Mom