當前位置: 首頁>>代碼示例>>C#>>正文


C# NpgsqlConnection.BeginTextImport方法代碼示例

本文整理匯總了C#中Npgsql.NpgsqlConnection.BeginTextImport方法的典型用法代碼示例。如果您正苦於以下問題:C# NpgsqlConnection.BeginTextImport方法的具體用法?C# NpgsqlConnection.BeginTextImport怎麽用?C# NpgsqlConnection.BeginTextImport使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Npgsql.NpgsqlConnection的用法示例。


在下文中一共展示了NpgsqlConnection.BeginTextImport方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CancelTextImport

        public void CancelTextImport()
        {
            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);

                var writer = (NpgsqlCopyTextWriter)conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
                writer.Write("HELLO\t1\n");
                writer.Cancel();
                Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data", conn), Is.EqualTo(0));
            }
        }
開發者ID:neisbut,項目名稱:npgsql,代碼行數:13,代碼來源:CopyTests.cs

示例2: Insert

        public void Insert(IEnumerable items, string tableName, Action<long> notifyRowsCopied = null,
            Action<Exception> onError = null)
        {
            var targetMethod = typeof (ObjectReader).GetMethod("Create", BindingFlags.Static | BindingFlags.Public);
            var targetGenericMethod = targetMethod.MakeGenericMethod(new Type[] {_type});

            var castMethod = typeof (Enumerable).GetMethod("Cast", BindingFlags.Static | BindingFlags.Public);
            var castGenericMethod = castMethod.MakeGenericMethod(new Type[] {_type});

            var destinationColumnsString =
                ColumnMappings.GetSqlBulkInsertMappings()
                    .Select(x => x.DestinationColumn)
                    .Aggregate((a, b) => a + "," + b);
            var sourceColumnsArray = ColumnMappings.GetSqlBulkInsertMappings().Select(x => x.SourceColumn).ToArray();

            var objectReader = (ObjectReader) targetGenericMethod.Invoke(null,
                new[] {castGenericMethod.Invoke(null, new[] {items}), sourceColumnsArray});

            using (var connection = new NpgsqlConnection(_connectionString))
            {

                try
                {
                    connection.Open();
                }
                catch (Exception x)
                {
                    Log.Error(x);
                    if (onError == null) throw;
                    onError(x);
                }

                var copyCommand = string.Format(CultureInfo.InvariantCulture, "COPY {0}({1}) FROM STDIN WITH CSV", tableName,
                    destinationColumnsString);

                using (var writer = connection.BeginTextImport(copyCommand))
                {

                    Log.DebugFormat("Starting bulk insert into '{0}'", tableName);
                    try
                    {
                        var row = 0;
                        IDataReader reader = objectReader as IDataReader;
                        while (reader.Read())
                        {
                            for (var i = 0; i < reader.FieldCount; i++)
                            {
                                if (i > 0)
                                    writer.Write(CsvDelimiter);
                                object value = reader.GetValue(i);
                                if (value != null)
                                {
                                    writer.Write(CsvQuote);
                                    writer.Write(_csvRegex.Replace(value.ToString(), CsvReplacement));
                                    writer.Write(CsvQuote);
                                }
                            }
                            writer.WriteLine();

                            row++;
                            if (row%NotifyAfter == 0)
                            {
                                if (notifyRowsCopied != null)
                                {
                                    notifyRowsCopied(row);
                                }
                            }
                        }
                        writer.Flush();
                    }
                    catch (Exception x)
                    {
                        Log.ErrorFormat("Error bulk inserting", x);
                        if (onError == null) throw;

                        onError(x);
                    }
                }
            }
        }
開發者ID:KyleGobel,項目名稱:Chronos,代碼行數:80,代碼來源:PostgresBulkInserter.cs

示例3: CloseDuringCopy

        public void CloseDuringCopy()
        {
            // TODO: Check no broken connections were returned to the pool
            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginBinaryImport("COPY data (field_text, field_int4) FROM STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginBinaryExport("COPY data (field_text, field_int2) TO STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) TO STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginTextExport("COPY data (field_text, field_int4) TO STDIN");
            }
        }
開發者ID:neisbut,項目名稱:npgsql,代碼行數:39,代碼來源:CopyTests.cs


注:本文中的Npgsql.NpgsqlConnection.BeginTextImport方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。