C# Prepared Statements - @ sign (at / strudel sign) queries

前端 未结 3 1616
滥情空心
滥情空心 2021-01-11 22:35

I Have a problem with a prepared statement in C#:

OdbcCommand cmd = sql.CreateCommand();
cmd.CommandText = \"SELECT UNIQUE_ID FROM userdetails WHERE USER_ID          


        
3条回答
  •  感情败类
    2021-01-11 22:59

    Indeed, ODBC has its share of issues with supporting named parameters. However, certain usage of named parameters is possible.

    For example, in your case the following syntax works:

    OdbcCommand cmd = sql.CreateCommand();
    cmd.CommandText = "SELECT UNIQUE_ID FROM userdetails WHERE USER_ID = ?";
    cmd.Parameters.Add("USER_ID", OdbcType.VarChar, 250).Value = email;
    

    More tricky situation is when you don't have a unique match for the parameter like USER_ID = ?; e.g., when you want to use the IN operator in the WHERE clause.

    Then the following syntax would do the job:

    OdbcCommand cmd = sql.CreateCommand();
    cmd.CommandText = "SELECT UNIQUE_ID FROM userdetails WHERE USER_ID IN (?, ?)";
    cmd.Parameters.Add("?ID1", OdbcType.VarChar, 250).Value = email1;
    cmd.Parameters.Add("?ID2", OdbcType.VarChar, 250).Value = email2;
    

    Please note the usage of ? (question mark) instead of @ (at sign) within the parameter name. Although note that substitution of parameters' values in this case has nothing to do with their names, but only with their order with the parameters collection.

    I hope this helps :-)

提交回复
热议问题