英文:
Inline If-else statement for an SQL query in C#
问题
这可能有点傻,但我试图将一个 if-else 语句简化为一个单字符串,使用内联 if 语句来进行 SQL 查询:
private void Populate_compoundDGV(int splitID = 0)
{
// SQL查询
string sql = "SELECT split_compound_id, " +
"CONCAT(nanogen_code, ' ', common_name) AS Compound, " +
"split_label AS Split, " +
"LOD, " +
"LOQ " +
"FROM compound INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id INNER JOIN split ON split_compound.split_id = split.split_id " +
"WHERE method_id = @methodID ";
// 如果splitID非零,添加额外的条件
if (splitID != 0)
{
sql += $"AND split.split_id = {splitID} ";
}
sql += "ORDER BY nanogen_code";
}
T-SQL 中有 CASE 语句,但这里可以使用参数来简化查询,就像上面的代码一样。
英文:
This might be silly, but I'm trying to reduce an if-else statement to a single string using an inline if-statement for an SQL query:
private void Populate_compoundDGV(int splitID = 0)
{
//sql
if (splitID==0)
{
string sql = "SELECT split_compound_id, " +
"CONCAT(nanogen_code, ' ', common_name) AS Compound, " +
"split_label AS Split, " +
"LOD, " +
"LOQ " +
"FROM compound INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id INNER JOIN split ON split_compound.split_id = split.split_id " +
"WHERE method_id = @methodID " +
"ORDER BY nanogen_code";
}
else
{
string sql = "SELECT split_compound_id, " +
"CONCAT(nanogen_code, ' ', common_name) AS Compound, " +
"split_label AS Split, " +
"LOD, " +
"LOQ " +
"FROM compound INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id INNER JOIN split ON split_compound.split_id = split.split_id " +
"WHERE method_id = @methodID " +
$"AND split.split_id = {splitID} " +
"ORDER BY nanogen_code";
}
}
I know T-SQL has a Case-statement. Would that work here? Can this be done with parameters or is there something simpler? I used to use an IIF statement in VB.Net.
答案1
得分: 2
private void Populate_compoundDGV(int splitID = 0)
{
var splitPredicate = splitID == 0 ? "" : $"AND s.split_id = @splitID";
string sql = $@"
SELECT split_compound_id,
CONCAT(nanogen_code, ' ', common_name) AS Compound,
split_label AS Split,
LOD,
LOQ
FROM compound c
INNER JOIN split_compound sc ON c.compound_id = sc.compound_id
INNER JOIN split s ON sc.split_id = s.split_id
WHERE method_id = @methodID
{splitPredicate}
ORDER BY nanogen_code";
}
英文:
You can do this:
private void Populate_compoundDGV(int splitID = 0)
{
var splitPredicate = splitID == 0 ? "" : "AND s.split_id = @splitID";
string sql = $@"
SELECT split_compound_id,
CONCAT(nanogen_code, ' ', common_name) AS Compound,
split_label AS Split,
LOD,
LOQ
FROM compound c
INNER JOIN split_compound sc ON c.compound_id = sc.compound_id
INNER JOIN split s ON sc.split_id = s.split_id
WHERE method_id = @methodID
{splitPredicate}
ORDER BY nanogen_code";
}
But you also need to check again when setting the query parameters. NEVER substitute data directly into an SQL command, not even for integers.
答案2
得分: 1
你可以使用一个单独的SQL语句和参数来处理所有内容:
SELECT split_compound_id,
CONCAT(nanogen_code, ' ', common_name) AS Compound,
split_label AS Split,
LOD,
LOQ
FROM compound
INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id
INNER JOIN split ON split_compound.split_id = split.split_id
WHERE method_id = @methodID
AND ( 0 = @splitID OR split.split_id = @splitID )
ORDER BY nanogen_code
英文:
You can handle all with a single SQL statement and parameters
SELECT split_compound_id,
CONCAT(nanogen_code, ' ', common_name) AS Compound,
split_label AS Split,
LOD,
LOQ
FROM compound
INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id
INNER JOIN split ON split_compound.split_id = split.split_id
WHERE method_id = @methodID
AND ( 0 = @splitID OR split.split_id = @splitID )
ORDER BY nanogen_code
答案3
得分: -1
以下是翻译好的部分:
- 使用 linq 和 EF 更好linq-where-with-and-or-condition。
- 如果您想使用 Tsql 查询来完成此操作,
最好将 SQL 服务器编写为 Procedure,然后使用 ef working-with-stored-procedure-in-ef-core 或 ado.net how-to-call-a-stored-procedure-using-ado-net,DynamicsqlWithAdo 来调用它。 - 但是,如果您想用 C# 编写,最好这样写(不建议使用此方法,因为它存在安全性、维护和开发问题 More Expling)
string sqlselect = "SELECT split_compound_id, " +
"CONCAT(nanogen_code, ' ', common_name) AS Compound, " +
"split_label AS Split, " +
"LOD, " +
"LOQ " +
"FROM compound INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id INNER JOIN split ON split_compound.split_id = split.split_id " +
"WHERE method_id = @methodID ";
string sqlorder = "ORDER BY nanogen_code ";
string sql = sqlselect + (splitID == 0 ? "" : $"AND split.split_id = {splitID} ") + sqlorder;
英文:
It is better to do this with linq and EF linq-where-with-and-or-condition
If you want to do it with Tsql queries,
it is better to write the sql server as a Procedure and call it with ef working-with-stored-procedure-in-ef-core or ado.net how-to-call-a-stored-procedure-using-ado-net , DynamicsqlWithAdo.
But if you want to write in C#, it is better to write like this(This method is not recommended because it has security and maintenance and development problems More Expling)
string sqlselect = "SELECT split_compound_id, " +
"CONCAT(nanogen_code, ' ', common_name) AS Compound, " +
"split_label AS Split, " +
"LOD, " +
"LOQ " +
"FROM compound INNER JOIN split_compound ON compound.compound_id = split_compound.compound_id INNER JOIN split ON split_compound.split_id = split.split_id " +
"WHERE method_id = @methodID ";
string sqlorder = "ORDER BY nanogen_code";
string sql = sqlselect+(splitID == 0 ? "" : $"AND split.split_id = {splitID} ") + sqlorder;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论