Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given how easy it is to replace this code, whilst removing the temptation to use dynamic SQL queries to pass parameters, I think the question should be: is there any benefit to keeping the code as-is?</p> <p>For example:</p> <pre><code>public static class Database { private static readonly Func&lt;DbCommandBuilder, int, string&gt; getParameterName = CreateDelegate("GetParameterName"); private static readonly Func&lt;DbCommandBuilder, int, string&gt; getParameterPlaceholder = CreateDelegate("GetParameterPlaceholder"); private static Func&lt;DbCommandBuilder, int, string&gt; CreateDelegate(string methodName) { MethodInfo method = typeof(DbCommandBuilder).GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] { typeof(Int32) }, null); return (Func&lt;DbCommandBuilder, int, string&gt;)Delegate.CreateDelegate(typeof(Func&lt;DbCommandBuilder, int, string&gt;), method); } private static string GetDefaultProviderName() { ... } private static string GetDefaultConnectionString() { ... } public static DbProviderFactory GetProviderFactory() { string providerName = GetDefaultProviderName(); return DbProviderFactories.GetFactory(providerName); } private static DbConnection GetDBConnection(DbProviderFactory factory) { DbConnection connection = factory.CreateConnection(); connection.ConnectionString = GetDefaultConnectionString(); return connection; } public static DbConnection GetDBConnection() { DbProviderFactory factory = GetProviderFactory(); return GetDBConnection(factory); } private static void ProcessParameters( DbProviderFactory factory, DbCommand command, string query, object[] queryParameters) { if (queryParameters == null &amp;&amp; queryParameters.Length == 0) { command.CommandText = query; } else { IFormatProvider formatProvider = CultureInfo.InvariantCulture; DbCommandBuilder commandBuilder = factory.CreateCommandBuilder(); StringBuilder queryText = new StringBuilder(query); for (int index = 0; index &lt; queryParameters.Length; index++) { string name = getParameterName(commandBuilder, index); string placeholder = getParameterPlaceholder(commandBuilder, index); string i = index.ToString("D", formatProvider); command.Parameters.AddWithValue(name, queryParameters[index]); queryText = queryText.Replace("{" + i + "}", placeholder); } command.CommandText = queryText.ToString(); } } public static DataView GetDataView(string query, params object[] queryParameters) { DbProviderFactory factory = GetProviderFactory(); using (DbConnection connection = GetDBConnection(factory)) using (DbCommand command = connection.CreateCommand()) { command.CommandType = CommandType.Text; ProcessParameters(factory, command, query, queryParameters); DbDataAdapter adapter = factory.CreateDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); return table.DefaultView; } } } </code></pre> <p>With this version, you can now pass in parameters simply and safely, without relying on custom code to try to block SQL injection:</p> <pre><code>DataView dv = Database.GetDataView( "select mycolumn from my table where id = {0} and name = {1}", 1234, "Robert');DROP TABLE Students;--"); </code></pre> <p><strong>EDIT</strong><br> Updated to support parameters for different providers, with help from <a href="https://stackoverflow.com/a/2550726/124386">this answer</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload