我必须在方法中用C#执行SQL Server查询。场景是:
该方法接受string category
的变量。如果category = "Heterogeneous"
的值,那么我必须选择:
SqlCommand myCommand = con.CreateCommand();
if (simCategory == "Heterogeneous")
{
myCommand.CommandText = @"SELECT ProbabilityHeterogeneous FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
}
else if (simCategory == "Low")
{
myCommand.CommandText = @"SELECT ProbabilityLow FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
}
else if (simCategory == "Medium")
{
myCommand.CommandText = @"SELECT ProbabilityMedium FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
}
else if (simCategory == "High")
{
myCommand.CommandText = @"SELECT ProbabilityHigh FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
}
myCommand.Parameters.AddWithValue("@sourceID", sID);
myCommand.Parameters.AddWithValue("@destinationID", dID);
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
while (myReader.Read())
{
inNeighborActivationProbability = Convert.ToDouble(myReader["Probability"]);
}
myReader.Close();
}
现在,这是真的吗?
答案 0 :(得分:0)
使用Switch
更清晰。您还需要在Probabilty
上使用别名。因此,无论用户选择哪一个,您都可以在最后调用它:myReader["Probability"]
最后尝试包含默认选项以防万一。
SqlCommand myCommand = con.CreateCommand();
switch (simCategory)
{
case "Heterogeneous":
myCommand.CommandText = @"SELECT ProbabilityHeterogeneous as Probability FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
break;
case "Low":
myCommand.CommandText = @"SELECT ProbabilityLow as Probability FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
break;
case "Medium":
myCommand.CommandText = @"SELECT ProbabilityMedium as Probability FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
break;
case "High":
myCommand.CommandText = @"SELECT ProbabilityHigh as Probability FROM Graph
WHERE SourceID = @sourceID AND DestinationID = @destinationID";
break;
default:
Console.WriteLine("Default case Rise ERROR?");
break;
}
myCommand.Parameters.AddWithValue("@sourceID", sID);
myCommand.Parameters.AddWithValue("@destinationID", dID);
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
while (myReader.Read())
{
inNeighborActivationProbability = Convert.ToDouble(myReader["Probability"]);
}
myReader.Close();
}
答案 1 :(得分:0)
我发现您的列名称类似于'概率*'并以simCategory结束,所以我建议下面的代码更清楚
注意:如果simCategory为null或为空,您还需要处理。
string sql = "";
switch (simCategory)
{
case "Heterogeneous":
case "Low":
case "Medium":
case "High":
sql=simCategory;
break;
default:
// handle error
break;
}
SqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = string.Format(@"SELECT Probability{0} as Probability FROM Graph WHERE SourceID =
@sourceID AND DestinationID = @destinationID", sql);
myCommand.Parameters.AddWithValue("@sourceID", sID);
myCommand.Parameters.AddWithValue("@destinationID", dID);
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
while (myReader.Read())
{
inNeighborActivationProbability = Convert.ToDouble(myReader["Probability"]);
}
myReader.Close();
}
答案 2 :(得分:0)
您的代码没有任何(真的)错误。您可能认为存在的原因是因为您似乎重复了很多代码并且没有感觉到正确'或者'高效的'。 如果你想决定在c#代码中使用哪个字段,那么你可以像这样做更高效的事情:
string fieldName;
switch (simCategory)
{
case "Heterogeneous":
fieldName = "ProbabilityHeterogeneous";
break;
case "Low":
fieldName = "ProbabilityLow";
break;
case "Medium":
fieldName = "ProbabilityMedium";
break;
case "High":
fieldName = "ProbabilityHigh";
break;
default:
throw new ArgumentOutOfRangeException(nameof(simCategory), "Unsupported Simulation Category requested.");
}
SqlCommand myCommand = con.CreateCommand();
myCommand.CommandText = string.Format("SELECT {0} as Probability FROM Graph WHERE SourceID = @sourceID AND DestinationID = @destinationID", fieldName);
myCommand.Parameters.AddWithValue("@sourceID", sID);
myCommand.Parameters.AddWithValue("@destinationID", dID);
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
etc...
如果您需要添加字段,这应该更易于阅读和维护。
如果出于某种原因,您更愿意在SQL中做出决策(例如,当您将逻辑放在存储过程中时,这样就可以扩展数据库模型和功能,而无需升级每个客户端机器你可以构建如下所示的SQL代码:
SELECT (CASE @parameter WHEN 'Heterogeneous' THEN ProbabilityHeterogeneous
WHEN 'Low' THEN ProbabilityLow
WHEN 'Medium' THEN ProbabilityMedium
WHEN 'High' THEN ProbabilityHigh
ELSE NULL END) as Probability
FROM Graph
WHERE SourceID = @sourceID
AND DestinationID = @destinationID
请注意,在这种情况下,错误的'参数值不会导致错误,但只会返回NULL作为值。