按姓名、地址、联系电话等搜索客户
Search Customer By Name, Address, Contact Number, Etc
我在根据姓名、地址、联系电话、支付金额等中输入的值搜索客户时卡住了
如果我只输入姓名,那么匹配 %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 中使用这些字段的所有可能组合???
或者有什么方法可以轻松完成这个任务?
在您的存储过程中使用此代码
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
缺点是即使输入错误,也可能得不到准确的结果。
您应该为未给出的值传递 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)
...
为where子句中的每个变量添加一个布尔参数:
(name LIKE @name AND @p1 = 1 OR @p1 = 0) AND (contactno LIKE @contactno AND @p2 = 1 OR @p2 = 0) AND ...
等等。
如果使用变量搜索,则将其对应的参数设为true
,否则设为false
.
优点:
这样您只需为任何类型的搜索编写一条命令。
您不必担心空值。
我在根据姓名、地址、联系电话、支付金额等中输入的值搜索客户时卡住了
如果我只输入姓名,那么匹配 %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 中使用这些字段的所有可能组合??? 或者有什么方法可以轻松完成这个任务?
在您的存储过程中使用此代码
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
缺点是即使输入错误,也可能得不到准确的结果。
您应该为未给出的值传递 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)
...
为where子句中的每个变量添加一个布尔参数:
(name LIKE @name AND @p1 = 1 OR @p1 = 0) AND (contactno LIKE @contactno AND @p2 = 1 OR @p2 = 0) AND ...
等等。
如果使用变量搜索,则将其对应的参数设为true
,否则设为false
.
优点:
这样您只需为任何类型的搜索编写一条命令。
您不必担心空值。