### Write DBF File with DBFWriter Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use DBFWriter to create a new DBF file. Define fields once, add records with AddRecord(), and flush to a stream using Write(). Null values are written as blank. Ensure proper stream and writer disposal. ```csharp using System; using System.IO; using System.Text; using DotNetDBF; string outputPath = "customers.dbf"; using (Stream fos = File.Open(outputPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var writer = new DBFWriter()) { // Set encoding for non-ASCII data (e.g., Cyrillic CP866) writer.CharEncoding = Encoding.GetEncoding(866); writer.Signature = DBFSigniture.DBase3; writer.LanguageDriver = 0x26; // CP866 writer.Fields = new[] { new DBFField("CUSTID", NativeDbType.Numeric, 6, 0), new DBFField("CUSTNAME", NativeDbType.Char, 50), new DBFField("BALANCE", NativeDbType.Numeric, 10, 2), new DBFField("INVODATE", NativeDbType.Date), new DBFField("ACTIVE", NativeDbType.Logical), }; writer.AddRecord(1, "Alice", 1500.75m, new DateTime(2024, 3, 15), true); writer.AddRecord(2, "Bob", 320.00m, new DateTime(2024, 4, 1), false); writer.AddRecord(3, "Charlie", DBNull.Value, null, true); // nulls ok writer.Write(fos); } // Output: customers.dbf written with 3 records ``` -------------------------------- ### Write DBF with Memo Fields (.DBT Sidecar) Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt To write Memo fields, set DataMemoLoc to the .dbt file path before assigning Fields. Wrap memo content in a MemoValue instance. The memo data is stored in a separate block-based sidecar file. ```csharp using System.IO; using DotNetDBF; string dbfPath = "notes.dbf"; string dbtPath = Path.ChangeExtension(dbfPath, "DBT"); using (Stream fos = File.Open(dbfPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var writer = new DBFWriter { DataMemoLoc = dbtPath }) { writer.Fields = new[] { new DBFField("TITLE", NativeDbType.Char, 40), new DBFField("CONTENT", NativeDbType.Memo), }; writer.AddRecord("Meeting Notes", new MemoValue("Discussed Q3 roadmap and budget allocations.")); writer.AddRecord("Project Summary", new MemoValue("Phase 1 complete. Phase 2 begins next sprint.")); writer.Write(fos); } // Produces notes.dbf + notes.DBT ``` -------------------------------- ### Dynamically Write Records to DBF with DotNetDBF Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use NewBlankRow to create a dynamic object matching the writer's field schema. Set properties by name and pass the row to writer.WriteRecord(IDBFInterceptor) to write it to the DBF file. ```csharp using System.IO; using DotNetDBF; using DotNetDBF.Enumerable; string path = "inventory.dbf"; using (var writer = new DBFWriter(path)) { writer.Fields = new[] { new DBFField("SKU", NativeDbType.Char, 20), new DBFField("QTY", NativeDbType.Numeric, 6, 0), new DBFField("PRICE", NativeDbType.Numeric, 10, 2), }; dynamic row = writer.NewBlankRow(); row.SKU = "ABC-001"; row.QTY = 100; row.PRICE = 9.99m; writer.WriteRecord(row); row = writer.NewBlankRow(); row.SKU = "XYZ-999"; row.QTY = 25; row.PRICE = 49.50m; writer.WriteRecord(row); } // inventory.dbf written with 2 records ``` -------------------------------- ### Writing DBF Files with dotnetdbf Source: https://github.com/ekonbenefits/dotnetdbf/blob/master/README.md Use this snippet to write data to DBF files. Configure character encoding, signature, and language driver before defining fields and adding records. ```csharp using (Stream fos = File.Open(dbffile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var writer = new DBFWriter()) { writer.CharEncoding = Encoding.GetEncoding(866); writer.Signature = DBFSigniture.DBase3; writer.LanguageDriver = 0x26; // Eq to CP866 var field1 = new DBFField("DOCDATE", NativeDbType.Date); var field2 = new DBFField("DOCNUMBER", NativeDbType.Char, 50); ... var field9 = new DBFField("f9", NativeDbType.Char, 20); writer.Fields = new[] { field1, field2, field3, field4, field5, field6, field7, field8, field9 }; foreach (var item in items) { writer.AddRecord( ... ); } writer.Write(fos); } ``` -------------------------------- ### Read DBF Memo Fields Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Set DataMemoLoc to point at the .dbt/.fpt sidecar before calling NextRecord(). Memo columns are returned as MemoValue objects; access the text via .Value (lazy-loaded on first access). ```csharp using System.IO; using DotNetDBF; using var reader = new DBFReader(File.OpenRead("notes.dbf")) { DataMemoLoc = "notes.DBT" }; object[] row; while ((row = reader.NextRecord()) != null) { var title = (string)row[0]; var content = row[1] is MemoValue memo ? memo.Value : string.Empty; Console.WriteLine($"{title}: {content}"); } // Meeting Notes: Discussed Q3 roadmap and budget allocations. // Project Summary: Phase 1 complete. Phase 2 begins next sprint. ``` -------------------------------- ### DBFEnumerable.NewBlankRow / WriteRecord Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Creates a dynamic object with properties matching the writer's field schema using `NewBlankRow()`. Properties can be set by name, and the resulting object can be passed to `writer.WriteRecord(IDBFInterceptor)` to write the record to the DBF file. ```APIDOC ## DBFEnumerable.NewBlankRow / WriteRecord — Dynamic Writer with Enumerable Extension `NewBlankRow()` creates a dynamic object with properties matching the writer's field schema. Set properties by name, then pass the row to `writer.WriteRecord(IDBFInterceptor)`. ```csharp using System.IO; using DotNetDBF; using DotNetDBF.Enumerable; string path = "inventory.dbf"; using (var writer = new DBFWriter(path)) { writer.Fields = new[] { new DBFField("SKU", NativeDbType.Char, 20), new DBFField("QTY", NativeDbType.Numeric, 6, 0), new DBFField("PRICE", NativeDbType.Numeric, 10, 2), }; dynamic row = writer.NewBlankRow(); row.SKU = "ABC-001"; row.QTY = 100; row.PRICE = 9.99m; writer.WriteRecord(row); row = writer.NewBlankRow(); row.SKU = "XYZ-999"; row.QTY = 25; row.PRICE = 49.50m; writer.WriteRecord(row); } // inventory.dbf written with 2 records ``` ``` -------------------------------- ### CharEncoding Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Controls how `Char` and `Memo` field bytes are interpreted using the `DBFBase.CharEncoding` property. The default is UTF-8. This can be overridden on both the reader and writer for legacy files using specific code pages. ```APIDOC ## CharEncoding — Custom Character Encoding `DBFBase.CharEncoding` (inherited by both `DBFReader` and `DBFWriter`) controls how `Char` and `Memo` field bytes are interpreted. The default is UTF-8. For legacy Clipper/FoxPro files using code pages, override this on both the reader and writer. ```csharp using System; using System.IO; using System.Text; using DotNetDBF; // Write with Windows-1251 (Cyrillic) using (Stream fos = File.Open("cyrillic.dbf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var writer = new DBFWriter { CharEncoding = Encoding.GetEncoding(1251) }) { writer.Fields = new[] { new DBFField("NAME", NativeDbType.Char, 40) }; writer.AddRecord("Привет мир"); // Hello World in Russian writer.Write(fos); } // Read back with the same encoding using (var reader = new DBFReader("cyrillic.dbf") { CharEncoding = Encoding.GetEncoding(1251) }) { object[] row = reader.NextRecord(); Console.WriteLine(row[0]); // Привет мир } ``` ``` -------------------------------- ### Define DBF Fields with DotNetDBF Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use DBFField to define column properties like name, type, length, and precision. Field names are limited to 10 characters. Fixed lengths are automatic for Date, Logical, and Memo types. ```csharp using DotNetDBF; // Char field, max 50 chars var nameField = new DBFField("CUSTNAME", NativeDbType.Char, 50); // Numeric field, 10 digits wide, 2 decimal places var balanceField = new DBFField("BALANCE", NativeDbType.Numeric, 10, 2); // Date field (length fixed at 8) var dateField = new DBFField("INVODATE", NativeDbType.Date); // Logical field (length fixed at 1) var activeField = new DBFField("ACTIVE", NativeDbType.Logical); // Memo field (length fixed at 10, stored in .dbt sidecar) var notesField = new DBFField("NOTES", NativeDbType.Memo); // Float field, 20 digits, 4 decimal places var rateField = new DBFField("RATE", NativeDbType.Float, 20, 4); Console.WriteLine(nameField.Name); // CUSTNAME Console.WriteLine(nameField.FieldLength); // 50 Console.WriteLine(balanceField.DecimalCount); // 2 ``` -------------------------------- ### Define 'dbase_83' Table Schema Source: https://github.com/ekonbenefits/dotnetdbf/blob/master/DotNetDBF.Test/dbfs/dbase_83_schema.txt This Ruby code defines the schema for a database table named 'dbase_83'. It includes columns for an ID, counts, order, codes, names, image-related paths, prices, costs, descriptions, weights, and boolean flags for taxable and active status. ```ruby ActiveRecord::Schema.define do create_table "dbase_83" do |t| t.column "id", :integer t.column "catcount", :integer t.column "agrpcount", :integer t.column "pgrpcount", :integer t.column "order", :integer t.column "code", :string, :limit => 50 t.column "name", :string, :limit => 100 t.column "thumbnail", :string, :limit => 254 t.column "image", :string, :limit => 254 t.column "price", :float t.column "cost", :float t.column "desc", :text t.column "weight", :float t.column "taxable", :boolean t.column "active", :boolean end end ``` -------------------------------- ### DBFEnumerable.DynamicAllRecords Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Reads all records from a DBF file and returns them as an IEnumerable. Each dynamic object allows access to field values as named properties. Supports optional filtering using whereColumn and whereColumnEquals for simple equality checks. ```APIDOC ## DBFEnumerable.DynamicAllRecords — Read All Records as Dynamic Objects `DynamicAllRecords()` (from `DotNetDBF.Enumerable`) reads all records and returns `IEnumerable` where each object exposes field values as named properties. Optionally filter with `whereColumn`/`whereColumnEquals` for simple equality queries. ```csharp using System.Linq; using DotNetDBF; using DotNetDBF.Enumerable; using var reader = new DBFReader("customers.dbf"); // Read all records dynamically foreach (dynamic record in reader.DynamicAllRecords()) { Console.WriteLine($"{record.CUSTNAME} owes {record.BALANCE}"); } // Filter by column value using var reader2 = new DBFReader("customers.dbf"); var activeCustomers = reader2.DynamicAllRecords(whereColumn: "ACTIVE", whereColumnEquals: true); Console.WriteLine($"Active count: {activeCustomers.Count()}"); // Active count: 2 ``` ``` -------------------------------- ### Read All Records as Dynamic Objects with DotNetDBF Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use DynamicAllRecords to read all DBF records into dynamic objects. Optionally filter records using whereColumn and whereColumnEquals for simple equality checks. ```csharp using System.Linq; using DotNetDBF; using DotNetDBF.Enumerable; using var reader = new DBFReader("customers.dbf"); // Read all records dynamically foreach (dynamic record in reader.DynamicAllRecords()) { Console.WriteLine($"{record.CUSTNAME} owes {record.BALANCE}"); } // Filter by column value using var reader2 = new DBFReader("customers.dbf"); var activeCustomers = reader2.DynamicAllRecords(whereColumn: "ACTIVE", whereColumnEquals: true); Console.WriteLine($"Active count: {activeCustomers.Count()}"); // Active count: 2 ``` -------------------------------- ### Read DBF Records Row by Row Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt DBFReader reads a DBF file from a file path or Stream. NextRecord() returns an object[] in field-declaration order, mapping DBF types to .NET types. Deleted records are automatically skipped. Returns null at end-of-file. ```csharp using System; using DotNetDBF; using var reader = new DBFReader("customers.dbf"); Console.WriteLine($"Total records: {reader.RecordCount}"); Console.WriteLine($"Fields: {string.Join(", ", Array.ConvertAll(reader.Fields, f => f.Name))}"); // Fields: CUSTID, CUSTNAME, BALANCE, INVODATE, ACTIVE object[] row; while ((row = reader.NextRecord()) != null) { var id = Convert.ToInt32(row[0]); var name = (string)row[1]; var balance = row[2] is DBNull ? (decimal?)null : (decimal)row[2]; var date = row[3] as DateTime?; var active = row[4] is DBNull ? (bool?)null : (bool)row[4]; Console.WriteLine($"[{id}] {name} | Balance: {balance:C} | Date: {date:d} | Active: {active}"); } // [1] Alice | Balance: $1,500.75 | Date: 3/15/2024 | Active: True // [2] Bob | Balance: $320.00 | Date: 4/1/2024 | Active: False // [3] Charlie | Balance: | Date: | Active: True ``` -------------------------------- ### Handle Custom Character Encoding in DotNetDBF Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Control character encoding for Char and Memo fields using DBFBase.CharEncoding. The default is UTF-8. Override this for legacy files using specific code pages, applying the same encoding to both reader and writer. ```csharp using System; using System.IO; using System.Text; using DotNetDBF; // Write with Windows-1251 (Cyrillic) using (Stream fos = File.Open("cyrillic.dbf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (var writer = new DBFWriter { CharEncoding = Encoding.GetEncoding(1251) }) { writer.Fields = new[] { new DBFField("NAME", NativeDbType.Char, 40) }; writer.AddRecord("Привет мир"); // Hello World in Russian writer.Write(fos); } // Read back with the same encoding using (var reader = new DBFReader("cyrillic.dbf") { CharEncoding = Encoding.GetEncoding(1251) }) { object[] row = reader.NextRecord(); Console.WriteLine(row[0]); // Привет мир } ``` -------------------------------- ### Read Only Specific DBF Columns Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Call SetSelectFields() with field names before reading to skip unnecessary columns, improving performance on wide tables. NextRecord() then returns an array sized to the selected fields only. ```csharp using DotNetDBF; using var reader = new DBFReader("customers.dbf"); reader.SetSelectFields("CUSTID", "CUSTNAME"); // Only read 2 of 5 fields object[] row; while ((row = reader.NextRecord()) != null) { Console.WriteLine($"ID={row[0]}, Name={row[1]}"); } // ID=1, Name=Alice // ID=2, Name=Bob // ID=3, Name=Charlie ``` -------------------------------- ### Read DBF Records into Strongly-Typed Objects Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use AllRecords to map DBF fields to a C# interface or anonymous type. Property names are matched case-insensitively; interface properties must match field names exactly. Unmapped fields are ignored. ```csharp using System.Linq; using DotNetDBF; using DotNetDBF.Enumerable; // Define an interface matching field names public interface ICustomer { string CUSTNAME { get; set; } decimal BALANCE { get; set; } } // --- Using interface --- using (var reader = new DBFReader("customers.dbf")) { foreach (ICustomer c in reader.AllRecords()) Console.WriteLine($"{c.CUSTNAME}: {c.BALANCE:C}"); // Alice: $1,500.75 // Bob: $320.00 } // --- Using anonymous type prototype (reads only matching fields) --- using (var reader = new DBFReader("customers.dbf")) { var rows = reader.AllRecords(new { CUSTID = default(decimal), CUSTNAME = default(string) }); Console.WriteLine(rows.First().CUSTNAME); // Alice } ``` -------------------------------- ### Append to Existing DBF File (RAF Mode) Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Use DBFWriter in Random Access File (RAF) mode to append records to an existing DBF file without buffering. This is suitable for large files or streaming inserts. Disposing the writer finalizes the header and writes the EOF marker. The header record count is updated on Dispose. ```csharp using DotNetDBF; string dbfPath = "orders.dbf"; // First pass: create the file with initial records if (!File.Exists(dbfPath)) { using var writer = new DBFWriter(dbfPath); writer.Fields = new[] { new DBFField("ORDERID", NativeDbType.Numeric, 8, 0), new DBFField("PRODUCT", NativeDbType.Char, 30), new DBFField("QTY", NativeDbType.Numeric, 5, 0), }; writer.WriteRecord(1001, "Widget A", 50); writer.WriteRecord(1002, "Widget B", 120); } // Dispose writes EOF marker // Second pass: append more records to the same file using (var writer = new DBFWriter(dbfPath)) { writer.WriteRecord(1003, "Gadget X", 30); writer.WriteRecord(1004, "Gadget Y", 75); } // Header record count updated on Dispose // File now contains 4 records ``` -------------------------------- ### DBFEnumerable.AllRecords Source: https://context7.com/ekonbenefits/dotnetdbf/llms.txt Maps DBF fields to a C# interface or anonymous type by matching property names (case-insensitive). Interface properties must match field names exactly. Unmapped fields are ignored. This method allows for strongly-typed record projection. ```APIDOC ## DBFEnumerable.AllRecords<T> — Strongly-Typed Record Projection `AllRecords()` maps DBF fields to a C# interface or anonymous type by matching property names (case-insensitive). Interface properties must match field names exactly. Unmapped fields are ignored. ```csharp using System.Linq; using DotNetDBF; using DotNetDBF.Enumerable; // Define an interface matching field names public interface ICustomer { string CUSTNAME { get; set; } decimal BALANCE { get; set; } } // --- Using interface --- using (var reader = new DBFReader("customers.dbf")) { foreach (ICustomer c in reader.AllRecords()) Console.WriteLine($"{c.CUSTNAME}: {c.BALANCE:C}"); // Alice: $1,500.75 // Bob: $320.00 } // --- Using anonymous type prototype (reads only matching fields) --- using (var reader = new DBFReader("customers.dbf")) { var rows = reader.AllRecords(new { CUSTID = default(decimal), CUSTNAME = default(string) }); Console.WriteLine(rows.First().CUSTNAME); // Alice } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.