|
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
namespace Research
{
public class DbCommandStore
{
public static DbCommandStore TheInstance = new DbCommandStore();
private Dictionary<string, DbCommand> _commandCache = new Dictionary<string, DbCommand>();
private DbCommandStore()
{
}
public DbCommand GetCommand(string cnHint)
{
ConnectionStringSettings cnSettings = DbConnectionStore.TheInstance.GetConnectionStringSettings(cnHint);
DbProviderFactory providerFactory = DbProviderFactories.GetFactory(cnSettings.ProviderName);
return providerFactory.CreateCommand();
}
public DbCommand GetCommand(string cnHint, string cmdText)
{
DbCommand cmd = null;
if (_commandCache.ContainsKey(cmdText.Trim()))
return _commandCache[cmdText];
ConnectionStringSettings cnSettings = DbConnectionStore.TheInstance.GetConnectionStringSettings(cnHint);
DbProviderFactory providerFactory = DbProviderFactories.GetFactory(cnSettings.ProviderName);
cmd = providerFactory.CreateCommand();
using (DbConnection cn = providerFactory.CreateConnection())
{
cn.ConnectionString = cnSettings.ConnectionString.Trim();
cn.Open();
cmd.CommandText = cmdText;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cn;
switch (cnSettings.ProviderName)
{
case "System.Data.SqlClient":
SqlCommandBuilder.DeriveParameters((SqlCommand)cmd);
break;
case "System.Data.OracleClient":
OracleCommandBuilder.DeriveParameters((OracleCommand)cmd);
break;
default:
throw new NotSupportedException("We may consider other providers in the future. Please let us know.");
}
}
_commandCache.Add(cmdText, cmd);
return cmd;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.