#pragma warning disable CA1822 // Mark members as static protected void ExecuteScript(Connection conn, string scriptText, ExportTableToDatabaseOptions options) #pragma warning restore CA1822 // Mark members as static { if (string.IsNullOrWhiteSpace(scriptText)) { return; } using var dataSet = new DataSet("Data"); var sqlScript = new SqlScript.SqlScript() { ScriptText = scriptText, Connection = conn?.DbConnection, ConnectionName = null, DataSet = dataSet, SCDispatcherService = null }; if (options.CommandTimeout.HasValue) { sqlScript.ScriptParameters.CommandTimeout = options.CommandTimeout.Value; } var dbConnections = DBConnections.LoadConnections(); sqlScript.RequestConnection += (s, e) => { var requestConnection = dbConnections.FindConnection(e.ConnectionName); if (requestConnection == null) { return; } var requestConn = new Connection(requestConnection.Provider, requestConnection.ConnectionString); if (requestConn.ConnectionType == null) { return; } requestConn.Open(); e.Connection = requestConn.DbConnection; }; sqlScript.ExecuteScript(); }
#pragma warning disable CA1822 // Mark members as static public DataSet InvokeSqlScript(DbConnection connection, string query, InvokeSqlScriptOptions options = null) #pragma warning restore CA1822 // Mark members as static { options ??= new InvokeSqlScriptOptions(); string scriptText = query; if (string.IsNullOrWhiteSpace(scriptText)) { throw new Exception("Cannot load script text."); } if ((connection?.State ?? ConnectionState.Closed) != ConnectionState.Open) { connection.Open(); } var dataSet = new DataSet("Data"); var sqlScript = new SqlScript.SqlScript() { ScriptText = scriptText, Connection = connection, ConnectionName = null, DataSet = dataSet, SCDispatcherService = null }; if (options.CommandTimeout.HasValue) { sqlScript.ScriptParameters.CommandTimeout = options.CommandTimeout.Value; } var dbConnections = DBConnections.LoadConnections(); sqlScript.ConnectionChanged += (s, e) => { }; sqlScript.RequestConnection += (s, e) => { var requestConnection = dbConnections.FindConnection(e.ConnectionName); if (requestConnection == null) { return; } var requestConn = new Connection(requestConnection.Provider, requestConnection.ConnectionString); if (requestConn.ConnectionType == null) { return; } requestConn.Open(); e.Connection = requestConn.DbConnection; }; if (options.Parameters != null) { foreach (DictionaryEntry keyPair in options.Parameters) { var parameterType = keyPair.Value?.GetType() ?? typeof(string); var parameter = new ScriptRunParameter() { Name = Convert.ToString(keyPair.Key), Type = parameterType, DbType = ScriptRunParameter.GetColumnDbType(parameterType), Value = keyPair.Value }; sqlScript.ScriptParameters.CommandParameters.Add(parameter); } } sqlScript.ExecuteScript(); return(dataSet); }