需要传递两个参数获得一个参数值

时间:2014-04-17 07:00:36

标签: asp.net sql-server stored-procedures

这是我的代码。我无法得到我想要的输出。我应该传递参数并使用CustomerCodeCustomerName检索。请帮我。在此先感谢,下面我放了我的sp

    protected void txtsearch_Click2(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(myStr);
            SqlCommand cmd = new SqlCommand("spRedemItem", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CustomerCode", SqlDbType.Int).Value = 
        (DropDownList2.SelectedItem.Text== "int" ? Convert.ToInt32(txtkey2.Text) :(object)DBNull.Value); 
            cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar, 50).Value = 
        (DropDownList2.SelectedItem.Text == "string" ? txtkey2.Text : (object)DBNull.Value);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            Session["CustomerName"] = dt;
            con.Open();
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dt = ds.Tables[0];
            Label10.Text = dt.Rows[0]["ItemCode"].ToString();
            Label11.Text = dt.Rows[0]["CustomerName"].ToString();
            Label12.Text = dt.Rows[0]["PointsNeeded"].ToString();
            GridView1.DataBind();
            con.Close();

        }

- 我的sp

alter Proc spRedemItem
(
@CustomerCode int=null,
@CustomerName nvarchar(50)=null
)
as
begin
select b.ItemCode,a.CustomerName,c.PointsNeeded from CustomerProfMain a
left join tb_Product b on a.CustomerCode=b.ItemCode
left join tb_RedemptionProducts c on b.Product_ID=c.ID
where (@CustomerCode is null or CustomerCode=@CustomerCode) or (@CustomerName is null or CustomerName=@CustomerName)
end

1 个答案:

答案 0 :(得分:0)

而不是使用,

where (@CustomerCode is null or CustomerCode=@CustomerCode) 
OR (@CustomerName is null or CustomerName=@CustomerName)

您可以尝试更改WHERE子句,例如

where (@CustomerCode is null or CustomerCode=@CustomerCode) 
AND
(@CustomerName is null or CustomerName=@CustomerName)