C# SQL查询c中的Where子句#
我正在尝试用c#编写一个SQL where子句。我有一个绑定数据库数据的网格。我在网格顶部有三个搜索面板,其中有三个文本框。即txtPartnumber、txtArticle、txtSmn。当我点击搜索按钮时,网格将被过滤。 E.x.以下是网格的过滤选项 1) 仅为搜索提供“零件号” 2) 仅为搜索提供“商品编号” 3) 仅为搜索提供“SMN” 4) “零件号”和“SMN”用于搜索等 为了使用SQL查询过滤网格中的数据,我尝试了以下代码。SQl中的Where子句似乎很难构建。附加我的代码和我的代码段似乎不是最复杂的代码。如何使用更少的代码行来最小化复杂性和优化代码C# SQL查询c中的Where子句#,c#,sql,C#,Sql,我正在尝试用c#编写一个SQL where子句。我有一个绑定数据库数据的网格。我在网格顶部有三个搜索面板,其中有三个文本框。即txtPartnumber、txtArticle、txtSmn。当我点击搜索按钮时,网格将被过滤。 E.x.以下是网格的过滤选项 1) 仅为搜索提供“零件号” 2) 仅为搜索提供“商品编号” 3) 仅为搜索提供“SMN” 4) “零件号”和“SMN”用于搜索等 为了使用SQL查询过滤网格中的数据,我尝试了以下代码。SQl中的Where子句似乎很难构建。附加我的代码和我的代
private string getCondtionForSearch()
{
string condition = string.Empty;
string whereCondition = " Where ";
string andCondition = " AND ";
string articleCondition = string.Empty;
string partNumberCondition = string.Empty;
string smnCondition = string.Empty;
int condtitionCount = 0;
if (!(string.IsNullOrEmpty(txtArticle.Text)))
{
articleCondition = string.Concat("ARTICLE = ", txtArticle.Text);
condtitionCount++;
}
if (!(string.IsNullOrEmpty(txtPartnumber.Text)))
{
partNumberCondition = string.Concat("PART_NUMBER = ", txtPartnumber.Text);
condtitionCount++;
}
if (!(string.IsNullOrEmpty(txtSmn.Text)))
{
smnCondition = string.Concat("SMN = ", txtSmn.Text);
condtitionCount++;
}
if (condtitionCount == 0)
condition = "SELECT * FROM [ItemMaster]";
else
{
StringBuilder conditionBuilder = new StringBuilder();
conditionBuilder.Append(whereCondition);
if (condtitionCount == 1)
{
conditionBuilder.Append(articleCondition);
conditionBuilder.Append(partNumberCondition);
conditionBuilder.Append(smnCondition);
}
if (condtitionCount == 2)
{
if (string.IsNullOrEmpty(articleCondition))
{
conditionBuilder.Append(partNumberCondition);
conditionBuilder.Append(andCondition);
conditionBuilder.Append(smnCondition);
}
}
conditionBuilder.Append(andCondition);
condition = conditionBuilder.ToString();
}
return condition;
}
感谢您在这方面的帮助。您可以通过检查每个值来构建where子句:请参见代码(但将其更改为使用StringBuilder,并将其更改为使用参数)
但是:您的代码容易受到SQL注入的攻击,因此请将此更改为使用SQL参数(Google)如果您要以这种方式构建查询,请尝试此方法。您应该使用参数化查询来避免
sql注入的可能性
string query = "SELECT * FROM [ItemMaster]"
string whereClause = string.empty;
bool andFlag = false;
if(!string.IsNullOrEmpty(txtArticle.Text))
{
string temp = " ARTICLE = @Article";
string.Concat(wherClause,temp);
andFlag = true;
//add parameter value for @Article
}
if(!string.IsNullOrEmpty(txtPartnumber.Text))
{
string temp = string.Empty;
if(andFlag)
temp = " AND PART_NUMBER = @Part_Number";
else
temp = " PART_NUMBER = @Part_Number";
string.Concat(whereClause ,temp);
//add parameter value for @Part_Number
}
if(!string.IsNullOrEmpty(txtSmn.Text))
{
string temp = string.Empty;
if(andFlag)
temp = " AND SMN = @SMN";
else
temp = " SMN = @SMN";
string.Concat(whereClause ,temp);
//add parameter value for @SMN
}
if(!string.IsNullOrEmpty(txtSmn.Text) || !string.IsNullOrEmpty(txtPartnumber.Text) || !string.IsNullOrEmpty(txtArticle.Text))
string.concat(query," WHERE ",whereClause);
您可以这样做(未测试):
这里是修改和简化版本的答案从编码器的代码
string query = "SELECT * FROM [ItemMaster] Where 1 = 1 "
if(!string.IsNullOrEmpty(txtArticle.Text))
{
string.Concat(query ,"AND ARTICLE = @Article ");
//add parameter value for @Article
}
if(!string.IsNullOrEmpty(txtPartnumber.Text))
{
string.Concat(query ,"AND PART_NUMBER = @Part_Number ");
//add parameter value for @Part_Number
}
if(!string.IsNullOrEmpty(txtSmn.Text))
{
string.Concat(query ,"AND SMN = @SMN ");
//add parameter value for @SMN
}
return query;
你想要sql查询还是c#linq?自己尝试一些你很了解的东西。谁知道你可能不需要我们。嗨,我需要一个SQL查询,我在上面尝试过获取SQL查询的Where子句。但是我觉得它很复杂,不是最佳的方法。你可能想在每个和之后添加空格,比如SMN=
应该是'SMN=',这样前面的值就不会得到带有列名的concat。所以如果我喜欢'SMN=',如果值在条件中为null,它会得到结果吗?如果是这样的话,这对我来说是一个简单的解决方案。谢谢你的代码。。我需要在where子句中有一个AND条件。我可以是2和条件,也可以是1,甚至没有。这是一个问题,我对在此基础上构建AND条件有任何线索……(谢谢你的帮助..是的,请。我的问题是Where子句可以有多个AND条件,甚至现在。需要处理它。请参阅我编辑的代码:你必须为此做很多工作,但它很简单。你添加了所有AND子句(不管是否有非或100…),检查是否添加了任何内容(非空),剪切最后一个“和”,并添加“WHERE”在where条件开始时,最好的方法是,更改函数以返回完整的SqlCommand,然后可以用@named parameters填充参数。还可以看到Jenish Rabadiya更简单的方法-我投票给他:-)是的,谢谢Thomas这是简单的方法谢谢你是天才
var sql = new StringBuilder();
sql.AppendLine("SELECT * FROM [ItemMaster]");
var whereClause = CheckFilter(txtArticle) +
CheckFilter(txtPartnumber) +
CheckFilter(txtSmn);
whereClause = whereClause.Substring(0, whereClause.Length - 5);
if (!string.IsNullOrEmpty(whereClause.Trim())
{
sql.AppendLine(" WHERE ");
sql.AppendLine(whereClause);
}
// using the textbox Name property as the column name
private string CheckFilter(TextBox textbox)
{
return !string.IsNullOrEmpty(textbox.Text)
? string.Format(" {0} = {1} AND ", textbox.Name, textbox.Text)
: string.Empty;
}
string query = "SELECT * FROM [ItemMaster] Where 1 = 1 "
if(!string.IsNullOrEmpty(txtArticle.Text))
{
string.Concat(query ,"AND ARTICLE = @Article ");
//add parameter value for @Article
}
if(!string.IsNullOrEmpty(txtPartnumber.Text))
{
string.Concat(query ,"AND PART_NUMBER = @Part_Number ");
//add parameter value for @Part_Number
}
if(!string.IsNullOrEmpty(txtSmn.Text))
{
string.Concat(query ,"AND SMN = @SMN ");
//add parameter value for @SMN
}
return query;