2017-03-06 124 views
1

我有一個存儲過程如下繁殖下拉列表中的值與值(和有一個表,一些列包括'Ta''Em''Ma''Sa'有幾種不同的記錄)創建存儲procudure數據庫

ALTER PROCEDURE [dbo].[Cstored] 
(
@Tar int, 
@Emk int, 
@Mas int, 
@San int 
) 
AS 
BEGIN 
    SET NOCOUNT ON; 
SELECT 
Namecar, 
Ta*@Tar AS val1, 
Em*@Emk as val2, 
Ma*@Mas as val3, 
Sa*@San As val4, 
(Ta*@Tar)+(Em*@Emk)+(Ma*@Mas)+(San*@San) as finalresult 
FROM Cdetail 
END 

和網頁包括一些下拉列表如下:

<form id="form1" runat="server"> 
     <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" > 
      <asp:ListItem Value="8">Emk</asp:ListItem> 
      <asp:ListItem Value="8">Tar</asp:ListItem> 
      <asp:ListItem Value="8">San</asp:ListItem> 
      <asp:ListItem Value="8">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="7">Emk</asp:ListItem> 
      <asp:ListItem Value="7">Tar</asp:ListItem> 
      <asp:ListItem Value="7">San</asp:ListItem> 
      <asp:ListItem Value="7">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="6">Emk</asp:ListItem> 
      <asp:ListItem Value="6">Tar</asp:ListItem> 
      <asp:ListItem Value="6">San</asp:ListItem> 
      <asp:ListItem Value="6">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:DropDownList ID="DropDownList4" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="6">Emk</asp:ListItem> 
      <asp:ListItem Value="6">Tar</asp:ListItem> 
      <asp:ListItem Value="6">San</asp:ListItem> 
      <asp:ListItem Value="6">Mas</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <br /> 
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 
     <br /> 
     <br /> 
     <asp:GridView ID="GridView1" runat="server"> 
     </asp:GridView> 
    </form> 

和下面的代碼背後:

protected void Button1_Click(object sender, EventArgs e) 
     { 

      DataTable dt; 
      string conStr = ConfigurationManager.ConnectionStrings["cConnectionString"].ConnectionString; 
      using (var sqlCon = new SqlConnection(conStr)) 
      { 
       sqlCon.Open(); 
       using (var scCommand = new SqlCommand("Cstored", sqlCon)) 
       { 
        scCommand.CommandType = CommandType.StoredProcedure; 
        scCommand.Parameters.Add("@Tar", SqlDbType.Int).Value = DropDownList1.SelectedValue; 
        scCommand.Parameters.Add("@Emk", SqlDbType.Int).Value = DropDownList2.SelectedValue; 
        scCommand.Parameters.Add("@Mas", SqlDbType.Int).Value = DropDownList3.SelectedValue; 
        scCommand.Parameters.Add("@San", SqlDbType.Int).Value = DropDownList4.SelectedValue; 
        using (SqlDataAdapter sda = new SqlDataAdapter(scCommand)) 
        { 
         dt = new DataTable(); 
         sda.Fill(dt); 
         GridView1.DataSource = dt; 
         GridView1.DataBind(); 
        } 
       } 
      } 

這些代碼的工作,但總是具有相同的結果,因爲這部分的

scCommand.Parameters.Add( 「@焦油」,SqlDbType.Int)。價值= DropDownList1.SelectedValue;

例如在DP1有幾個選擇與價值8,如果用戶選擇任何內容,將在存儲過程'@Tar'相乘,它同其他DP,因爲它的結果是一樣始終即使有不同選擇,但我想如果用戶選擇'Emk'DP1值8只與'@Emk'在存儲過程中相乘,如果選擇'Mas'DP 2,它乘以'@Mas'。 但我不知道如何在代碼中定義它,您的幫助將不勝感激。

回答

0

改變你的存儲過程作爲這個樣子,

DECLARE @dp1 varchar (100) 
DECLARE @dp2 varchar (100) 
DECLARE @dp3 varchar (100) 
DECLARE @dp4 varchar (100) 

DECLARE @dpp1 int 
DECLARE @dpp2 int 
DECLARE @dpp3 int 
DECLARE @dpp4 int 


set @dp1 = 'Emk' >>as example 
set @dp2 = 'Mas' >>as example 



if (@dp1= 'Emk') 
Begin 
set @dpp1= 8 
Select Emk*@dpp1 as val1 FROM Cdetail 
End 

if (@dp1= 'Tar') 
Begin 
set @dpp1= 8 
Select Tar*@dpp1 as val1 FROM Cdetail 
End 

if (@dp1= 'San') 
Begin 
set @dpp1= 8 
Select San*@dpp1 as val1 FROM Cdetail 
End 

if (@dp1= 'Mas') 
Begin 
set @dpp1= 8 
Select Mas*@dpp1 as val1 FROM Cdetail 
End 

if (@dp2= 'Emk') 
Begin 
set @dpp2= 7 
Select Emk*@dpp2 as val2 FROM Cdetail 
End 

if (@dp2= 'Tar') 
Begin 
set @dpp2= 7 
Select Tar*@dpp2 as val2 FROM Cdetail 
End 

if (@dp2= 'San') 
Begin 
set @dpp2= 7 
Select San*@dpp2 as val2 FROM Cdetail 
End 

if (@dp2= 'Mas') 
Begin 
set @dpp2= 7 
Select Mas*@dpp2 as val1 FROM Cdetail 
End 


SELECT 
Namecar, 
(Mas*@dpp1)+(San*@dpp1)+(Tar*@dpp1)+(Emk*@dpp1)+(Mas*@dpp2)+(San*@dpp2)+(Tar*@dpp2)+(Emk*@dpp2) 
as finalresult 
FROM Cdetail 

是注意這是一個例子,你需要改進它