我在根据姓名、地址、联系电话、付款金额等输入的值搜索客户时陷入了困境。
如果我仅输入名称,则所有匹配 %name%
的客户应显示在下面。
如果我输入姓名和地址,则应显示符合该条件的客户。
我有搜索字段
1) 姓名
2) 联系电话
3) 身份类型(ddl,带有 PAN CARD、VOTING CARD、PASSPORT 等选项)
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 中使用这些字段的所有可能组合??? 或者有什么办法可以轻松完成这个任务吗?
最佳答案
对于未给出的值,您应该传递 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)
...
关于c# - 按姓名、地址、联系电话等搜索客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738869/