Note that there are some explanatory texts on larger screens.

plurals
  1. POParameter naming using System.Data.Common
    primarykey
    data
    text
    <p>This is probably an oldie-but-goodie. I am using System.Data.Common for an interchangeable Oracle/SQL Server/SQLite data access library. During the constructor I take the connection string name and use that to determine the underlying provider type. The reason I do this is to handle the different IDbParameter naming conventions for each provider. For example, Oracle likes :parameter whereas SQL Server and SQLite like @parameter. The default is ? to cover Oledb.</p> <p>Question: Is this all unnecessary and is there some simple thing I'm missing that should simply take care of this? If my IDbCommand.CommandText = "select id, name from my.table where id = :id" am I covered? For now I'm just adopting ? as the default and then RegEx'ing my way to the right parameter identifier before executing the command.</p> <p>Thanks.</p> <pre><code> /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="RelationalGateway"/&gt; class. /// &lt;/summary&gt; /// &lt;remarks&gt;You must pass in the name of the connection string from the application configuration /// file rather than the connection string itself so that the class can determine /// which data provider to use, e.g., SqlClient vs. OracleClient.&lt;/remarks&gt; public RelationalGateway(string connectionStringName) { if (string.IsNullOrEmpty(connectionStringName)) throw new ArgumentNullException("connectionStringName"); if (ConfigurationManager.ConnectionStrings[connectionStringName] == null || ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString.Length == 0 || ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName.Length == 0) { throw new InvalidOperationException(string.Format( "The configuration file does not contain the {0} connection ", connectionStringName) + "string configuration section or the section contains empty values. Please ensure the " + "configuration file has the appropriate values and try again."); } _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; _providerName = ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName; _theProvider = DbProviderFactories.GetFactory(_providerName); _adapter = _theProvider.CreateDataAdapter(); //GetConnection(); DetermineProviderSpecificParameters(); } </code></pre> <p>The DetermineProviderSpecificParameters bit basically figures out "?" or ":" or "@" or something else.</p> <p>UPDATE Here's how I'm handling the details thus far:</p> <ol> <li><p>Get the right parameter string:</p> <p>private void DetermineProviderSpecificParameters() { // Check for supported providers. This is so that parametized queries to limit // by spatial extent are correctly created. string shortName = _providerName.Substring(_providerName.LastIndexOf(".") + 1);</p> <pre><code> switch (shortName) { case "SqlClient": _param = "@"; _ql = "["; _qr = "]"; break; case "SQLite": _param = "@"; _ql = string.Empty; _qr = string.Empty; break; case "OracleClient": _param = ":"; _ql = string.Empty; _qr = string.Empty; break; default: _param = "?"; _ql = string.Empty; _qr = string.Empty; break; } } </code></pre></li> <li><p>call a little helper before I execute each command to "cleanify" or "parameterific" it or however we call this half-assed hack:</p> <pre><code>private void MakeProviderSpecific(IDbCommand command) { foreach (IDataParameter param in command.Parameters) { param.ParameterName = GetProviderSpecificCommandText(param.ParameterName); } command.CommandText = GetProviderSpecificCommandText(command.CommandText); } </code></pre></li> <li><p>And this calls a little regex to do to do:</p> <pre><code>public string GetProviderSpecificCommandText(string rawCommandText) { return Regex.Replace(rawCommandText, @"\B\?\w+", new MatchEvaluator(SpecificParam)); } </code></pre></li> </ol> <p>Yuck. Still searching for a relatively simple solution but the advice thus far is certainly appreciated.</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.
 

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