目标:在查询中使用应用设置中的字符串
代码:
private SqlConnection sqlconn = new SqlConnection();
private String[] strSomeValue = ConfigurationManager.AppSettings["SomeValue"].ToString().Split(';');
String strSQL = Type.SelectedValue;
SqlCommand cmd = SqlConnection.CreateCommand();
if (strSQL == "SomeValue")
{
cmd.CommandText = @"SELECT Value
FROM Types
WHERE "Some Value"
}
我们的想法是使用应用程序设置中的字符串填充查询的“部分值”部分。提前感谢您的任何参考,意见和建议
答案 0 :(得分:1)
const string query =
"SELECT Value"
+ "FROM Types"
+ "WHERE Value = @SomeValue";
using(var command = connection.CreateCommand())
{
command.CommandText = query;
command.Parameters.AddWithValue("@SomeValue", strSomeValue[0]);
// TODO: open connection, execute command, get result
}