문제

Objective: use a string from app settings within a query

Code:

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"
  }

The idea is to have the Some Value portion of the query be filled with the string from app settings. Thank you in advance for any references, comments and suggestions

도움이 되었습니까?

해결책

Use a parameterized query:

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top