System.Data.SqlClient.SqlConnection myConnection = new SqlConnection(ConnectionString)
public DbConnection GetConnection(String connectionString) { // Cration de la connection DbConnection conn = this.DbProviderFactory.CreateConnection(); // Rcupration de la chaine de connexion connectionStringSettings = connectionStringsCollection[connectionStringName]; // Affectation de la chaine de connexion conn.ConnectionString = connectionStringSettings.ConnectionString; // retour de la connexion return conn; }
public DbCommand CreateDbCommand(String commandText, CommandType commandType) { DbCommand command = this.DbProviderFactory.CreateCommand(); command.CommandType = commandType; command.CommandText = commandText; command.Connection = GetConnection(); return command; }
/// <summary> /// Excute un ordre T-SQL et renvoit le nombre de lignes affectes /// </summary> public int ExecuteNonQuery(String commandText) { int resultat; DbConnection conn = null; try { conn = this.GetConnection(); DbCommand command = this.CreateDbCommand(commandText, CommandType.Text); command.Connection = conn; conn.Open(); resultat = command.ExecuteNonQuery(); } finally { if (conn.State != System.Data.ConnectionState.Closed) conn.Close(); } // Retour du rsultat; return resultat; }
/// <summary> /// Excute un ordre T-SQL et renvoit une valeur /// </summary> /// <param name="CommandText">Requte xcuter</param> /// <returns>Valeur de retour</returns> public Object ExecuteScalar(String commandText) { Object resultat = null; DbConnection conn = null; try { conn = this.GetConnection(); DbCommand command = this.CreateDbCommand(commandText, CommandType.Text); conn.Open(); resultat = command.ExecuteScalar(); } finally { if (conn.State != System.Data.ConnectionState.Closed) conn.Close(); } // Retour du rsultat; return resultat; }
public DbDataReader GetReader(String commandText) { DbDataReader dataReader = null; DbConnection conn = null; try { conn = this.GetConnection(); DbCommand command = this.CreateDbCommand(commandText, CommandType.Text); conn.Open(); dataReader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } catch { if (conn.State != System.Data.ConnectionState.Closed) conn.Close(); } // Retour du dataReader; return dataReader; }
public void GetDataSet(DataSet dataSet, String tableName, String commandText) { DbConnection conn = null; DbDataReader dbreader = null; try { conn = this.GetConnection(); DbCommand command = this.CreateDbCommand(commandText, conn, CommandType.Text); conn.Open(); dbreader = command.ExecuteReader(CommandBehavior.CloseConnection); dataSet.Load(dbreader, LoadOption.PreserveChanges, new string[] { tableName }); } finally { if (conn.State != System.Data.ConnectionState.Closed) conn.Close(); } }
connexion.Open(); System.Data.SqlClient.SqlCommandBuilder.DeriveParameters(command); connexion.Close();
private void DeriveParameters(DbCommand command, DbConnection connexion) { if (command is System.Data.SqlClient.SqlCommand) { connexion.Open(); System.Data.SqlClient.SqlCommandBuilder.DeriveParameters((System.Data.SqlClient.SqlCommand)command); connexion.Close(); } else if (command is System.Data.Odbc.OdbcCommand) { connexion.Open(); System.Data.Odbc.OdbcCommandBuilder.DeriveParameters((System.Data.Odbc.OdbcCommand)command); connexion.Close(); } else if (command is System.Data.OleDb.OleDbCommand) { connexion.Open(); System.Data.OleDb.OleDbCommandBuilder.DeriveParameters((System.Data.OleDb.OleDbCommand)command); connexion.Close(); } else { // --------------------------------------- // Tentative de rflexion // --------------------------------------- try { DbCommandBuilder commandBuilder = DbProviderFactory.CreateCommandBuilder(); // Rcupration du Type du commandBuilder cre par mon Provider Type commandBuilderType = commandBuilder.GetType(); // Tentative de rcupration de la mthode DeriveParameters MethodInfo methodInfo = commandBuilderType.GetMethod("DeriveParameters"); // Si la mthode n'existe pas, exeception leve if (methodInfo == null) throw (new Exception("DeriveParameters method is not suppored by the selected provider")); // Invocation de la mthode DeriveParameters methodInfo.Invoke(null, new object[1] { command }); } catch (Exception ex) { throw ex; } } }
DbParameterCollection parametresLocaux = null; if (!cache.ContainsKey(command.CommandText)) { lock (cache) { if (!cache.ContainsKey(command.CommandText)) { this.DeriveParameters(commandeLocale, connexionLocale); if (this.CacheEnabled) { cache.Add(command.CommandText, commandeLocale.Parameters); } else { parametresLocaux = commandeLocale.Parameters; } } } }