X-Received: by 10.224.185.201 with SMTP id cp9mr18552048qab.6.1362420234657; Mon, 04 Mar 2013 10:03:54 -0800 (PST) X-Received: by 10.49.63.164 with SMTP id h4mr2144619qes.39.1362420234496; Mon, 04 Mar 2013 10:03:54 -0800 (PST) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!dd2no7000802qab.0!news-out.google.com!q17ni7qal.0!nntp.google.com!dd2no7000796qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.databases.berkeley-db Date: Mon, 4 Mar 2013 10:03:54 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=131.167.254.100; posting-account=YBQ26goAAAB26kVRjRwREwUxwx-rKOx7 NNTP-Posting-Host: 131.167.254.100 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Help: Read Berkeley Key,Value using C# From: ggkraemer@gmail.com Injection-Date: Mon, 04 Mar 2013 18:03:54 +0000 Content-Type: text/plain; charset=ISO-8859-1 X-Received-Bytes: 9076 Xref: csiph.com comp.databases.berkeley-db:17 I'm new to C#. I have installed Berkeley 5.3.21 using the distribution msi. Can someone please tell me how to correct my C# program to decode a recNo Berkeley database. I can do it using C++; but I'm having problems understanding how to retrieve/decode the key,value pairs using C#. TIA Garry -------------------- Code ------------------------- // //define Btree false // // Berkeley database is a recNo database with key,value pair defined as: // key - unsigned int (1-4095) // value - structure _bdbcomponentrecord (same as class defined below) // using System; using System.IO; using System.Collections.Generic; using System.Collections; using System.Diagnostics; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using BerkeleyDB; using System.Runtime.InteropServices; namespace AccessingBerkeley { // // Replaces struct _bdbcomponentrecord // public class _bdbcomponentrecord { public int maintenanceaction; public int componentstate; public uint componentcode; public int componenttype; public string componentname = new string(new char[51]); public byte rsvd; // Alignment public int enginetype; // public static string CopyFrom(DatabaseEntry dbt) // { // System.Text.ASCIIEncoding decode = new ASCIIEncoding(); // return decode.GetString(dbt.Data); // } } class Program { static void Main(string[] args) { #if Btree BTreeDatabase btreeDB; BTreeDatabaseConfig btreeConfig; #else RecnoDatabase recnoDB; RecnoDatabaseConfig recnoConfig; #endif Cursor dbc; DatabaseEntry key; DatabaseEntry data; string buff, dbFileName, keyString, keyStr; int keyVal; int compNo; int recNo; _bdbcomponentrecord rec = new _bdbcomponentrecord(); try { String pwd = Environment.CurrentDirectory; pwd = Path.Combine(pwd, ".."); pwd = Path.Combine(pwd, ".."); if (IntPtr.Size == 4) pwd = Path.Combine(pwd, "Win32"); else pwd = Path.Combine(pwd, "x64"); #if DEBUG pwd = Path.Combine(pwd, "Debug"); #else pwd = Path.Combine(pwd, "Release"); #endif pwd += ";" + Environment.GetEnvironmentVariable("PATH"); Environment.SetEnvironmentVariable("PATH", pwd); } catch (Exception e) { Console.WriteLine( "Unable to set the PATH environment variable."); Console.WriteLine(e.Message); return; } try { #if Btree dbFileName = @"C:\BDB\componen.bdb"; #else dbFileName = @"C:\BDB\componen.bdb"; #endif if (args.Length > 0) { dbFileName = args[0]; } } catch { usage(); return; } #if false if (File.Exists(dbFileName)) { while (true) { Console.Write ("{0} already exists. Delete it? (y/n) ", dbFileName); buff = Console.ReadLine().ToLower(); if (buff == "y" || buff == "n") break; } if (buff == "y") { try { File.Delete(dbFileName); } catch { Console.WriteLine("Unable to delete {0}.", dbFileName); return; } } } #endif #if Btree btreeConfig = new BTreeDatabaseConfig(); btreeConfig.Duplicates = DuplicatesPolicy.SORTED; btreeConfig.ErrorPrefix = "excs_access"; btreeConfig.Creation = CreatePolicy.IF_NEEDED; btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1); btreeConfig.PageSize = 8 * 1024; #else recnoConfig = new RecnoDatabaseConfig(); recnoConfig.ErrorPrefix = "excs_access"; recnoConfig.Creation = CreatePolicy.IF_NEEDED; recnoConfig.CacheSize = new CacheInfo(0, 32 * 1024, 1); recnoConfig.PageSize = 1024; #endif try { #if Btree btreeDB = BTreeDatabase.Open(dbFileName, btreeConfig); #else recnoDB = RecnoDatabase.Open(dbFileName, recnoConfig); #endif } catch (Exception e) { Console.WriteLine("Error opening {0}.", dbFileName); Console.WriteLine(e.Message); Console.Write("\nPress any key to exit >"); Console.ReadKey(true); return; } key = new DatabaseEntry(); data = new DatabaseEntry(); #if false while (true) { Console.Write("key [blank line to quit] > "); keyString = Console.ReadLine(); if (keyString == "") break; keyVal = Convert.ToInt32(keyString); keyStr = Convert.ToString(keyVal); dbtFromString(key, keyStr); dbtFromString(data, reverse(keyString)); try { #if Btree btreeDB.Put(key, data); #else recnoDB.PutNoOverwrite(key, data); #endif } catch { return; } } #endif #if Btree using (dbc = btreeDB.Cursor()) { Console.WriteLine("All key : data pairs:"); foreach (KeyValuePair p in dbc) { Console.WriteLine("{0}::{1}", strFromDBT(p.Key), strFromDBT(p.Value)); } } } #else using (dbc = recnoDB.Cursor()) { Console.WriteLine("All key : data pairs:"); foreach (KeyValuePair p in dbc) { //bdbcomponentrecvar.CopyFrom(p.Value); compNo = Convert.ToInt32(p.Key); Console.WriteLine("{0}", compNo); } } #endif Console.Write("Press any key to exit >"); Console.ReadKey(true); #if Btree btreeDB.Close(); #else recnoDB.Close(); #endif } #region Utilities public static void usage() { Console.WriteLine( "Usage: excs_access [database]"); } static void dbtFromString(DatabaseEntry dbt, string s) { dbt.Data = System.Text.Encoding.ASCII.GetBytes(s); } public static string strFromDBT(DatabaseEntry dbt) { System.Text.ASCIIEncoding decode = new ASCIIEncoding(); return decode.GetString(dbt.Data); } public static string reverse(string s) { StringBuilder tmp = new StringBuilder(s.Length); for (int i = s.Length - 1; i >= 0; i--) tmp.Append(s[i]); return tmp.ToString(); } #endregion Utilities } } -------------------- End Code -------------------------