Exemple #1
0
        protected override void BeginProcessing()
        {
            var opener = new ConnectionSpecifier(FileOrName, Connection, null, null);

            _connection       = opener.Connection;
            _connectionOpened = opener.ConnectionOpened;

            try
            {
                if (_connection == null)
                {
                    WriteError(new ErrorRecord(new RuntimeException("Can't open a connection"), "", ErrorCategory.NotSpecified, null));
                    throw new PipelineStoppedException();
                }

                // ODBC and OLEDB Access connections fail to obtain the corresponding factories.
                if (_connection is System.Data.Odbc.OdbcConnection)
                {
                    _factory = DbProviderFactories.GetFactory("System.Data.Odbc");
                }
                else if (_connection is System.Data.OleDb.OleDbConnection)
                {
                    _factory = DbProviderFactories.GetFactory("System.Data.OleDb");
                }
                else
                {
                    _factory = DbProviderFactories.GetFactory(_connection);
                }

                if (_factory == null)
                {
                    WriteError(new ErrorRecord(new RuntimeException("Failed to obtain a DbProviderFactory object"), "", ErrorCategory.NotSpecified, null));
                    throw new PipelineStoppedException();
                }

                _builder = _factory.CreateCommandBuilder();

                // Supply a command builder with an adaptor object because some providers' builders
                // (including those of ODBC and OLEDB Access) require an active connection to make QuoteIndentifier() work.
                using (var adaptor = _factory.CreateDataAdapter())
                    using (var cmd = _connection.CreateCommand())
                    {
                        cmd.CommandText       = "select 1";
                        adaptor.SelectCommand = cmd;
                        _builder.DataAdapter  = adaptor;
                    }

                _useNamedParameters = false;
            }
            catch (Exception e)
            {
                DisposeResources();

                WriteError(new ErrorRecord(e, "", ErrorCategory.NotSpecified, null));
                throw new PipelineStoppedException();
            }
        }
Exemple #2
0
        protected override void EndProcessing()
        {
            try {
                var opener = new ConnectionSpecifier(FileOrName, null, ProviderName, ConnectionString);

                WriteObject(opener.Connection);
            }
            catch (Exception ex) {
                WriteError(new ErrorRecord(ex, "", ErrorCategory.NotSpecified, null));
            }
        }
Exemple #3
0
        protected override void EndProcessing()
        {
            var opener         = new ConnectionSpecifier(FileOrName, Connection, null, null);
            var connection     = opener.Connection;
            var connectionOpen = opener.ConnectionOpened;

            try
            {
                DataTable schema;
                if (CollectionName != null && CollectionName != "")
                {
                    schema = connection.GetSchema(CollectionName);
                }
                else
                {
                    schema = connection.GetSchema();
                }

                try
                {
                    foreach (var row in schema.Rows)
                    {
                        WriteObject(row);
                    }
                }
                finally
                {
                    schema.Dispose();
                }
            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(ex, "", ErrorCategory.NotSpecified, null));
            }
            finally
            {
                if (connectionOpen)
                {
                    connection.Close();
                    connection.Dispose();
                }
            }
        }
Exemple #4
0
        protected override void EndProcessing()
        {
            var  opener           = new ConnectionSpecifier(FileOrName, Connection, null, null);
            var  connection       = opener.Connection;
            bool connectionOpened = opener.ConnectionOpened;

            try {
                using (DbCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = Query;

                    if (MyInvocation.BoundParameters.ContainsKey("Timeout"))
                    {
                        cmd.CommandTimeout = Timeout;
                    }

                    if (Parameters != null)
                    {
                        if (Parameters is IDictionary dictParam)
                        {
                            foreach (DictionaryEntry entry in dictParam)
                            {
                                object value;
                                if (entry.Value is PSObject psobj)
                                {
                                    value = psobj.BaseObject;
                                }
                                else
                                {
                                    value = entry.Value;
                                }

                                var param = cmd.CreateParameter();
                                param.ParameterName = (string)entry.Key;
                                param.Value         = value;
                                cmd.Parameters.Add(param);
                            }
                        }
                        else
                        {
                            ICollection parameters;
                            if (Parameters is ICollection col)
                            {
                                parameters = col;
                            }
                            else
                            {
                                parameters = new object[] { Parameters }
                            };

                            foreach (var v in parameters)
                            {
                                object value;
                                if (v is PSObject psobj)
                                {
                                    value = psobj.BaseObject;
                                }
                                else
                                {
                                    value = v;
                                }

                                var param = cmd.CreateParameter();
                                param.Value = value;
                                cmd.Parameters.Add(param);
                            }
                        }
                    }

                    if (AsDataRow || AsDataTable)
                    {
                        var factory = DbProviderFactories.GetFactory(connection);
                        using (var adaptor = factory.CreateDataAdapter())
                            using (var dataSet = new DataSet())
                            {
                                adaptor.SelectCommand = cmd;
                                adaptor.Fill(dataSet);
                                GetDataQueryResult.RecordsAffected = -1;

                                if (AsDataTable)
                                {
                                    WriteObject(dataSet.Tables[0]);
                                }
                                else
                                {
                                    foreach (var row in dataSet.Tables[0].Rows)
                                    {
                                        WriteObject(row);
                                    }
                                }
                            }
                    }
                    else
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            GetDataQueryResult.RecordsAffected = reader.RecordsAffected;

                            var count = reader.FieldCount;

                            string[] fieldNames = new string[count];
                            var      nameHash   = new HashSet <string>();
                            for (int i = 0; i < count; ++i)
                            {
                                var name = reader.GetName(i);

                                var suffix = 1;
                                while (nameHash.Contains(name))
                                {
                                    name = reader.GetName(i) + suffix++;
                                }

                                fieldNames[i] = name;
                                nameHash.Add(name);
                            }

                            while (reader.Read())
                            {
                                var obj       = new PSObject();
                                var exprCount = 1;
                                for (int i = 0; i < count; ++i)
                                {
                                    object value = null;
                                    if (PreserveDbNull || !reader.IsDBNull(i))
                                    {
                                        value = reader.GetValue(i);
                                    }

                                    PSNoteProperty prop;
                                    try
                                    {
                                        prop = new PSNoteProperty(fieldNames[i], value);
                                    }
                                    catch (PSArgumentException)
                                    {
                                        prop = new PSNoteProperty("Expr" + exprCount, value);
                                        ++exprCount;
                                    }
                                    obj.Properties.Add(prop);
                                }
                                WriteObject(obj);
                            }
                        }
                    }

                    if (ShowRecordsAffected)
                    {
                        WriteObject(GetDataQueryResult.RecordsAffected);
                    }
                }
            }
            catch (Exception e) {
                WriteError(new ErrorRecord(e, "", ErrorCategory.NotSpecified, null));
            }
            finally {
                if (connectionOpened)
                {
                    connection.Close();
                    connection.Dispose();
                }
            }
        }