按姓名,地址,联系电话等搜索客户

时间:2015-01-02 07:27:27

标签: c# sql winforms stored-procedures

根据在姓名,地址,联系电话,付款金额等中输入的值,我在搜索客户方面遇到困难。

如果我只输入姓名,则所有符合%name%的客户都应显示在下方。 如果我输入姓名和地址,则应显示符合该条件的客户。

我有搜索字段

1)姓名

2)联系号码

3)身份类型(带有PAN CARD,VOTING CARD,PASSPORT等选项的ddl)

4)身份证号码

5)地址

6)金额标准(ddl,期权等于,小于,大于,之间)

7)支付金额(如果等于,小于,大于)

8)最低金额(如果介于两者之间)

9)最大金额(如果介于两者之间)

我试过

using (SqlCommand cmd = new SqlCommand("sp_search_customer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                if (txtCustomerName.Text != "" && txtContactNumber.Text != "" && cbIDProofType.Text != "" && txtIDNumber.Text != "" && txtAddress.Text != "" && txtAmountPaid.Text == "")
                {
                    cmd.Parameters.Add(new SqlParameter("@customer_name", SqlDbType.NVarChar)).Value = name;
                    cmd.Parameters.Add(new SqlParameter("@customer_address", SqlDbType.NVarChar)).Value = address;
                    cmd.Parameters.Add(new SqlParameter("@customer_contact_number", SqlDbType.Int)).Value = contact_number;
                    cmd.Parameters.Add(new SqlParameter("@customer_id_proof_type", SqlDbType.NVarChar)).Value = id_proof_type;
                    cmd.Parameters.Add(new SqlParameter("@customer_id", SqlDbType.NVarChar)).Value = id_number;
                    cmd.Parameters.Add(new SqlParameter("@amount_paid", SqlDbType.Int)).Value = 0;
                    cmd.Parameters.Add(new SqlParameter("@min_amount", SqlDbType.Int)).Value = min_amount;
                    cmd.Parameters.Add(new SqlParameter("@max_amount", SqlDbType.Int)).Value = max_amount;
                }
                else if (txtCustomerName.Text != "" && txtContactNumber.Text != "" && cbIDProofType.Text != "" && txtIDNumber.Text != "" && txtAddress.Text != "" && txtMinAmount.Text != "" && txtMaxAmount.Text != "")
                {
                    cmd.Parameters.Add(new SqlParameter("@customer_name", SqlDbType.NVarChar)).Value = name;
                    cmd.Parameters.Add(new SqlParameter("@customer_address", SqlDbType.NVarChar)).Value = address;
                    cmd.Parameters.Add(new SqlParameter("@customer_contact_number", SqlDbType.Int)).Value = contact_number;
                    cmd.Parameters.Add(new SqlParameter("@customer_id_proof_type", SqlDbType.NVarChar)).Value = id_proof_type;
                    cmd.Parameters.Add(new SqlParameter("@customer_id", SqlDbType.NVarChar)).Value = id_number;
                    cmd.Parameters.Add(new SqlParameter("@amount_paid", SqlDbType.Int)).Value = amount_paid;
                    cmd.Parameters.Add(new SqlParameter("@min_amount", SqlDbType.Int)).Value = 0;
                    cmd.Parameters.Add(new SqlParameter("@max_amount", SqlDbType.Int)).Value = 0;
                }

                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(DatTab);
            }

            this.customerBindingSource1.DataSource = DatTab;
            this.reportViewer1.RefreshReport();

我的存储过程是

ALTER PROCEDURE [dbo].[sp_search_customer]

@customer_name nvarchar(100),
@customer_address nvarchar(500),
@customer_contact_number nvarchar(50),
@customer_id_proof_type nvarchar(50),
@customer_id nvarchar(50),
@amount_paid int,
@min_amount_paid int,
@max_amount_paid int

as
begin
    select customer_number, customer_name, customer_address, customer_contact_number, customer_id_proof_type, customer_id, customer_vehicle_type, customer_vehicle_number, customer_room_number, customer_no_of_days_stay, customer_cost, check_in_date, number_of_adults, number_of_children
from Customer
where Customer.customer_name LIKE ''+'%'+@customer_name+'%'+''
    and Customer.customer_address LIKE ''+'%'+@customer_address+'%'+''
    AND Customer.customer_contact_number = @customer_contact_number
    AND Customer.customer_id_proof_type = @customer_id_proof_type
    AND Customer.customer_id = @customer_id
    AND Customer.customer_cost = @amount_paid
    AND Customer.customer_cost BETWEEN @min_amount_paid and @max_amount_paid
end

但结果不如预期。 如果我在每种情况下都使用AND,那么它就不会显示任何记录。 如果我在每种情况下使用OR,那么它会显示所有记录。 你能告诉我怎么做吗?

我是否必须在if ... else中使用这些字段的所有可能组合? 或者有什么方法可以轻松完成这项任务吗?

3 个答案:

答案 0 :(得分:1)

您应该为未给出的值传递NULL:

if (String.IsNullOrEmpty(name))
{ 
  cmd.Parameters.Add(new SqlParameter("@customer_name", SqlDbType.NVarChar)).Value = DBNull.Value;
}
else
{ 
  cmd.Parameters.Add(new SqlParameter("@customer_name", SqlDbType.NVarChar)).Value = name;
}

然后在存储过程中检查NULL:

where (Customer.customer_name LIKE ''+'%'+@customer_name+'%'+'' or @customer_name is null)
and (Customer.customer_address LIKE ''+'%'+@customer_address+'%'+'' or @customer_address is null)
...

答案 1 :(得分:0)

在存储过程中使用此代码

BEGIN
    SELECT customer_number, customer_name, customer_address, customer_contact_number, customer_id_proof_type, customer_id, customer_vehicle_type, customer_vehicle_number, customer_room_number, customer_no_of_days_stay, customer_cost, check_in_date, number_of_adults, number_of_children
from Customer
where   Customer.customer_contact_number = @customer_contact_number
    AND Customer.customer_id_proof_type = @customer_id_proof_type
    AND Customer.customer_id = @customer_id
    AND Customer.customer_cost = @amount_paid
    AND Customer.customer_cost BETWEEN @min_amount_paid 
    AND @max_amount_paid
    OR Customer.customer_name LIKE ''+'%'+@customer_name+'%'+''
    OR Customer.customer_address LIKE ''+'%'+@customer_address+'%'+''
END

缺点是,即使一个条目出错,您也可能无法获得准确的结果。

答案 2 :(得分:0)

为where子句中的每个变量添加一个布尔参数:

(name LIKE @name AND @p1 = 1 OR @p1 = 0) AND (contactno LIKE @contactno AND @p2 = 1 OR @p2 = 0) AND ...等等。

如果您使用变量进行搜索,请将与其对应的参数设置为true,否则设置为false

优点:

  1. 这样您只需为任何类型的搜索只编写一个命令。

  2. 您不必担心空值。