Table name and table field on SqlParameter C#?

前端 未结 3 626
面向向阳花
面向向阳花 2020-12-01 20:58

I would like to know how to pass the table name and a table field name via SqlCommand on C#.

Tryied to do it the way it\'s done by setting the SqlCommand with the @

3条回答
  •  情书的邮戳
    2020-12-01 21:45

    If you are worried about SQL injection, the SqlCommandBuilder class (and other DB specific versions of DbCommandBuilder) have a function called QuoteIdentifier that will escape your table name properly.

    var builder = new SqlCommandBuilder();
    string escTableName = builder.QuoteIdentifier(tableName);
    

    Now you can used the escaped value when building your statement and not have to worry about injection- but you should still be using parameters for any values.

提交回复
热议问题