Esempio n. 1
0
        /// <summary>
        /// writes content of data queue into output DBF file
        /// </summary>
        /// <param name="queue"></param>
        protected override void DoWrite(IDataQueue queue)
        {
            var dbf       = new DbfFile();
            var formatter = new BedValueFormatter(FormatSettings);

            try
            {
                if (File.Exists(GetWorkingFileName()))
                {
                    File.Delete(GetWorkingFileName());
                }
                dbf.Create(GetWorkingFileName());

                ITableStructure ts = queue.GetRowFormat;
                foreach (var col in ts.Columns)
                {
                    DbfColumn.DbfColumnType type;
                    int len = 0, scale = 0;
                    switch (col.DataType.Code)
                    {
                    case DbTypeCode.Array:
                    case DbTypeCode.Generic:
                    case DbTypeCode.Text:
                    case DbTypeCode.Xml:
                        type = DbfColumn.DbfColumnType.Memo;
                        break;

                    case DbTypeCode.Blob:
                        type = DbfColumn.DbfColumnType.Binary;
                        break;

                    case DbTypeCode.Datetime:
                        var dtype = (DbTypeDatetime)col.DataType;
                        if (dtype.SubType == DbDatetimeSubType.Date)
                        {
                            type = DbfColumn.DbfColumnType.Date;
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Character;
                            len  = DateTime.UtcNow.ToString("s").Length;
                        }
                        break;

                    case DbTypeCode.Float:
                        type  = DbfColumn.DbfColumnType.Number;
                        len   = 18;
                        scale = DefaultNumericScale;
                        break;

                    case DbTypeCode.Int:
                        if (AllowFoxProInteger)
                        {
                            type = DbfColumn.DbfColumnType.Integer;
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Number;
                            len  = 18;
                        }
                        break;

                    case DbTypeCode.Logical:
                        type = DbfColumn.DbfColumnType.Boolean;
                        break;

                    case DbTypeCode.Numeric:
                        type  = DbfColumn.DbfColumnType.Number;
                        len   = 18;
                        scale = ((DbTypeNumeric)col.DataType).Scale;
                        break;

                    case DbTypeCode.String:
                        var stype = (DbTypeString)col.DataType;
                        if (stype.IsBinary)
                        {
                            type = DbfColumn.DbfColumnType.Binary;
                        }
                        else if (stype.Length <= 254)
                        {
                            type = DbfColumn.DbfColumnType.Character;
                            len  = stype.Length;
                            if (len <= 0)
                            {
                                len = DefaultStringLength;
                            }
                        }
                        else
                        {
                            type = DbfColumn.DbfColumnType.Memo;
                        }
                        break;

                    default:
                        type = DbfColumn.DbfColumnType.Character;
                        len  = DefaultStringLength;
                        break;
                    }
                    dbf.Header.AddColumn(col.ColumnName, type, len, scale);
                }

                var orec = new DbfRecord(dbf.Header);
                while (!queue.IsEof)
                {
                    var record = queue.GetRecord();
                    orec.Clear();
                    for (int i = 0; i < ts.Columns.Count; i++)
                    {
                        record.ReadValue(i);
                        formatter.ReadFrom(record);
                        orec[i] = formatter.GetText();
                    }
                    dbf.Write(orec);
                }
            }
            finally
            {
                dbf.Close();
                queue.CloseReading();
            }
            FinalizeBulkCopy();
        }
Esempio n. 2
0
        private static void TestWriteNewDbf()
        {
            //create a simple DBF file and output to args[0]
            var odbf = new DbfFile(Encoding.GetEncoding(1252));

            odbf.Open(Path.Combine(TestPath, "TestNew2.dbf"), FileMode.Create);

            //create a header
            odbf.Header.AddColumn(new DbfColumn("StrCol", DbfColumn.DbfColumnType.Character, 20, 0));
            odbf.Header.AddColumn(new DbfColumn("DecCol1", DbfColumn.DbfColumnType.Number, 5, 1));
            odbf.Header.AddColumn(new DbfColumn("DecCol2", DbfColumn.DbfColumnType.Number, 5, 2));
            odbf.Header.AddColumn(new DbfColumn("DecCol3", DbfColumn.DbfColumnType.Number, 5, 3));
            odbf.Header.AddColumn(new DbfColumn("DecCol4", DbfColumn.DbfColumnType.Number, 15, 5));
            odbf.Header.AddColumn(new DbfColumn("NumCol1", DbfColumn.DbfColumnType.Number, 5, 0));
            odbf.Header.AddColumn(new DbfColumn("NumCol2", DbfColumn.DbfColumnType.Number, 10, 0));
            odbf.Header.AddColumn(new DbfColumn("DateCol1", DbfColumn.DbfColumnType.Date));
            odbf.Header.AddColumn(new DbfColumn("BoolCol1", DbfColumn.DbfColumnType.Boolean));

            //add some records...
            var orec = new DbfRecord(odbf.Header)
            {
                AllowDecimalTruncate = true
            };

            orec[0] = "Ahmed Test";
            orec[1] = "123.5";
            orec[2] = "12.35";
            orec[3] = "1.235";
            orec[4] = "1235.123456";
            orec[5] = "1235";
            orec[6] = "123567890";
            orec[7] = "11/07/2007";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer";
            orec[1] = "-1.5";
            orec[2] = "-1.35";
            orec[3] = "1.235";
            orec[4] = "-1235.123";
            orec[5] = "15";
            orec[6] = "12345";             //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "2008-12-21";
            orec[8] = "f";
            odbf.Write(orec, true);

            orec[0] = "Stéfanié Singer longer than fits in the DBF record!";
            orec[1] = "0.1";
            orec[2] = ".12";
            orec[3] = ".1";
            orec[4] = "";
            orec[5] = "-15";
            orec[6] = "-12345";             //put a decimal in integer, we won't throw an exception beacuse we do not test for that.
            orec[7] = "";
            orec[8] = "no";
            odbf.Write(orec);

            //overwrite first record with last record's data...
            orec.RecordIndex = 0;
            odbf.Write(orec);

            //odbf.Header.RecordCount = 50;
            odbf.WriteHeader();

            odbf.Close();


            //open the same DBF file we just output, and append a few records to it...
            odbf.Open(Path.Combine(TestPath, "TestNew2.dbf"), FileMode.Open);

            orec.Clear();
            orec[0] = "New record added!";
            orec[6] = "100";
            orec[8] = "t";
            odbf.Write(orec, true);

            orec[0] = "New record 2";
            orec[6] = "104";
            orec[8] = "y";
            odbf.Write(orec, true);

            orec[0] = "New record 3";
            orec[6] = "104";
            orec[8] = "TRUE";
            odbf.Write(orec, true);

            if (odbf.Read(0, orec))
            {
                orec[0] = "modified first record";
                odbf.Write(orec, true);
            }

            //read 3rd record and output to console...
            Console.WriteLine(odbf.Read(2).ToString());


            //now add a new record, forcing seek to end of file...
            orec.Clear();
            orec[0] = "New record 4";
            orec[6] = "500";
            orec[8] = "FALSE";
            odbf.Write(orec, true);


            odbf.Close();

            Console.ReadKey();
        }