This project has moved. For the latest updates, please go here.

Writing data to a file

Sep 27, 2013 at 7:15 PM
Hi,
I want to use OpenHistorian 2.0 as my local historian. In the documentation, there are two ways to write data:

From Sample1 - Write Archive Points.cs
  public void WriteDataToAFile()
  {
        string fileName = @"C:\Temp\ArchiveFile.d2";
        if (File.Exists(fileName))
            File.Delete(fileName);
        var key = new HistorianKey();
        var value = new HistorianValue();
        using (var file = ArchiveFile.CreateFile(fileName))
        using (var table = file.OpenOrCreateTable<HistorianKey, HistorianValue>(CreateFixedSizeNode.TypeGuid))
        using (var editor = table.BeginEdit())
        {
            key.TimestampAsDate = DateTime.Now;
            key.PointID = 1;
            value.AsString = "Test Write";
            editor.AddPoint(key, value);
            editor.Commit();
        }
    }
and from Sample3 - Single Database Client.cs
    public void CreateScadaDatabase()
    {
        Array.ForEach(Directory.GetFiles(@"c:\temp\Scada\", "*.d2", SearchOption.AllDirectories), File.Delete);

        HistorianDatabaseInstance db = new HistorianDatabaseInstance();
        db.IsNetworkHosted = false;
        db.InMemoryArchive = false;
        db.ConnectionString = "port=12345";
        db.Paths = new[] { @"c:\temp\Scada\" };

        var key = new HistorianKey();
        var value = new HistorianValue();

        using (var server = new HistorianServer(db))
        {
            HistorianClientOptions clientOptions = new HistorianClientOptions();
            clientOptions.NetworkPort = 12345;
            clientOptions.ServerNameOrIp = "127.0.0.1";

            using (var client = new HistorianClient<HistorianKey, HistorianValue>(clientOptions))
            {
                var database = client.GetDefaultDatabase();

                for (ulong x = 0; x < 1000; x++)
                {
                    key.Timestamp = x;
                    database.Write(key, value);
                }

                database.HardCommit();
                System.Threading.Thread.Sleep(1200);
                database.Disconnect();
            }
        }
    }
Which is the preferred method?

Renan
Developer
Sep 27, 2013 at 7:32 PM
These two samples show how to write data to a single archive file, or to a historian server. The HistorianServer is socket based, therefore it allows clients to remotely connect to it.

Unless you are looking to write individual archive files (such as converting an existing database), I recommend that you use sample 3.