C# 尝试忽略Winforms中的空文本框以筛选sql搜索查询

C# 尝试忽略Winforms中的空文本框以筛选sql搜索查询,c#,sql,winforms,C#,Sql,Winforms,目前,我正在尝试保留一个持久的where子句,然后通过追加每个文本框是否存在来追加额外的过滤器。问题是这给了我一个很好的命令,但我在尝试使用该命令时仍然收到一个错误。如果有任何简化或指导,将不胜感激 if (string.IsNullOrEmpty(make) && (string.IsNullOrEmpty(model)) && (string.IsNullOrEmpty(color)) && (string.IsNullOrEmpty(min)

目前,我正在尝试保留一个持久的where子句,然后通过追加每个文本框是否存在来追加额外的过滤器。问题是这给了我一个很好的命令,但我在尝试使用该命令时仍然收到一个错误。如果有任何简化或指导,将不胜感激

if (string.IsNullOrEmpty(make) && (string.IsNullOrEmpty(model)) && (string.IsNullOrEmpty(color)) && (string.IsNullOrEmpty(min)) && (string.IsNullOrEmpty(max)) && (string.IsNullOrEmpty(miles)))
            {
                SqlCommand updateDataGridViewCmd = new SqlCommand("select m.make, m.model, car.price, color.color, car.mileage, carlot.lotid, car.pic from car join makemodel as m ON m.mmid = car.mmid join color ON  car.colorid = color.colorid join carlot ON  carlot.carid = car.carid; ", sqlCon);

                dt.Load(updateDataGridViewCmd.ExecuteReader());
                dataGridView1.DataSource = dt;
            }
            else
            {
                StringBuilder sqlCommandText = new StringBuilder();
                sqlCommandText.Append("select m.make, m.model, car.price, color.color, car.mileage, carlot.lotid, car.pic from car join makemodel as m ON m.mmid = car.mmid join color ON  car.colorid = color.colorid join carlot ON  carlot.carid = car.carid where");
                string CommandText = sqlCommandText.ToString();

                SqlCommand updateDataGridViewCmd = new SqlCommand(CommandText, sqlCon);
                updateDataGridViewCmd.Parameters.AddWithValue("@make", make);
                updateDataGridViewCmd.Parameters.AddWithValue("@model", model);
                updateDataGridViewCmd.Parameters.AddWithValue("@min", min);
                updateDataGridViewCmd.Parameters.AddWithValue("@max", max);
                updateDataGridViewCmd.Parameters.AddWithValue("@mileage", miles);
                updateDataGridViewCmd.Parameters.AddWithValue("@color", color);



                if (!string.IsNullOrEmpty(make))
                {
                    sqlCommandText.Append(" m.make = @make");
                    CommandText = sqlCommandText.ToString();
                }

                if (!string.IsNullOrEmpty(model))
                {
                    sqlCommandText.Append(" OR m.model = @model");
                    CommandText = sqlCommandText.ToString();
                }

                if (!string.IsNullOrEmpty(min))
                {
                    sqlCommandText.Append(" car.price between @min");
                    CommandText = sqlCommandText.ToString();
                    if (!string.IsNullOrEmpty(max))
                    {
                        sqlCommandText.Append(" AND @max");
                        CommandText = sqlCommandText.ToString();

                    }
                    else 
                    {
                        sqlCommandText.Append(",");
                        CommandText = sqlCommandText.ToString();
                    }
                }

                if (!string.IsNullOrEmpty(color))
                {
                    sqlCommandText.Append(" color.color = @color,");
                    CommandText = sqlCommandText.ToString();
                }

                if (!string.IsNullOrEmpty(miles))
                {
                    sqlCommandText.Append(" car.price <= @mileage");
                    CommandText = sqlCommandText.ToString();
                }
                sqlCommandText.Append(";");
                CommandText = sqlCommandText.ToString();
                dt.Load(updateDataGridViewCmd.ExecuteReader());
                dataGridView1.DataSource = dt;
            }

                
        }
    }
if(string.IsNullOrEmpty(make)&&&(string.IsNullOrEmpty(model))&&&(string.IsNullOrEmpty(color))&&&(string.IsNullOrEmpty(min))&&(string.IsNullOrEmpty(max))&&(string.IsNullOrEmpty(miles)))
{
SqlCommand UPDATEDATAGRIDVIEWMD=new SqlCommand(“选择m.make、m.model、car.price、color.color、car.MILENGE、CARROT.lotid、car.pic从car join makemodel中选择m.mmid=car.mmid join color.colorid=car.CARRID;”,sqlCon);
Load(updateDagDagRidViewCmd.ExecuteReader());
dataGridView1.DataSource=dt;
}
其他的
{
StringBuilder sqlCommandText=新建StringBuilder();
sqlCommandText.Append(“从car join makemodel中选择m.make、m.model、car.price、color.color、car.Miledge、carlot.lotid、car.pic作为m ON m.mmid=car.mmid join color ON car.colorid=color.colorid join carlot ON carlot.carid=car.carid where”);
string CommandText=sqlCommandText.ToString();
SqlCommand updateDataGridViewCmd=新的SqlCommand(CommandText,sqlCon);
UpdatedTagRidViewCmd.Parameters.AddWithValue(“@make”,make);
UpdateDataRidViewCmd.Parameters.AddWithValue(“@model”,model);
UpdatedTagRidViewCmd.Parameters.AddWithValue(“@min”,min);
UpdatedTagRidViewCmd.Parameters.AddWithValue(“@max”,max);
UpdateDagDagRidViewCmd.Parameters.AddWithValue(“@里程”,英里);
UpdateDataRidViewCmd.Parameters.AddWithValue(“@color”,color);
如果(!string.IsNullOrEmpty(make))
{
sqlCommandText.Append(“m.make=@make”);
CommandText=sqlCommandText.ToString();
}
如果(!string.IsNullOrEmpty(模型))
{
sqlCommandText.Append(“或m.model=@model”);
CommandText=sqlCommandText.ToString();
}
如果(!string.IsNullOrEmpty(min))
{
sqlCommandText.Append(“car.price-between@min”);
CommandText=sqlCommandText.ToString();
如果(!string.IsNullOrEmpty(max))
{
sqlCommandText.Append(“AND@max”);
CommandText=sqlCommandText.ToString();
}
其他的
{
sqlCommandText.Append(“,”);
CommandText=sqlCommandText.ToString();
}
}
如果(!string.IsNullOrEmpty(颜色))
{
sqlCommandText.Append(“color.color=@color,”);
CommandText=sqlCommandText.ToString();
}
如果(!string.IsNullOrEmpty(英里))
{

sqlCommandText.Append(“car.price您可能会看到一个错误,因为您没有将条件字符串与

如果ORM在这里不是一个选项(这在组合查询时可能很实用),您可以使用
结束基本查询,其中1=1
,然后使用
和x=…
或x=…

链接其他条件
您还可以设置
CommandText=sqlCommandText.ToString()
只有一次,在应用了所有过滤器之后

考虑这是一个伪代码。我在这个编辑器中从头开始写的。基本上,不需要处理无休止的条件,而是创建一个对象,将你的条件分组,然后将这些对象的实例的输出分组。它应该为你构建一个完美的过滤器

公共类令牌
{
私有列表_localColl;
//这里申报的私人物品
privat bool_之间;
public WhereToken(字符串列、对象[]值、SqlDbType t、SqlParameterCollection参数列)
{
//在这里指派士兵
}
public WhereToken(字符串列、对象值1、对象值2、SqlDbType t、SqlParameterCollection参数列)
{
//在这里指派士兵
_介于=真之间;
_values=新对象[]{value1,value2};
}
公共字符串写入()
{
如果(values.Length=0)
返回null;
_localColl=新列表();
var b=新的StringBuilder();
b、 附加(“”);
对于(int i=0;l<_values.Length;i++)
{
var pName=string.Concat(“@”,col,i);
var p=新的SqlParameter(pName,值[i],_sqlType);
b、 附加(_col);
如果(_-between)
b、 附加(“之间”);
其他的
b、 附加(“=”);
b、 追加(pName);
如果(iSELECT ... FROM TABLECUSTOMERS WHERE <predicate1> OR <predicate2> OR ...
if (inputStringName != null)
{
   strBuilder.Append("OR CustomerName = @Name")
}
AND (@Name == null OR @Name == CustomerName)
SELECT ... FROM car
JOIN ...
WHERE ( NOT @Make = NULL AND @Make = m.Make)
   OR ( NOT @Model = NULL AND @Model = m.Model)
   OR ( NOT @Mileage = NULL AND @Mileage > car.Mileage)