Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
5

Use C# SqlFile to get the Energy Plus result

asked 2015-07-31 01:26:21 -0600

zhengangzhu's avatar

updated 2015-11-07 14:21:52 -0600

Using the following C# code, there is always a System.AccessViolationException

Path path = new Path("D:\\IdealAirSystem.sql");

SqlFile sqlFile = new SqlFile(path);

Is this the correct way to use SqlFile? (The sql file exists at that path)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2015-07-31 09:49:19 -0600

That is the correct way to use both Path and SqlFile.

Here is a short command line program I used to test this...

using System;
using OpenStudio;

namespace openstudio_test
{
    class Program
    {
        static void Main(string[] args)
        {
            Path path = new Path("C:\\EnergyPlusV8-3-0\\ExampleFiles\\RefBldgMediumOfficeNew2004_Chicago.sql"); // change to path of your sql file
            Console.WriteLine(path.__str__());
            SqlFile sqlFile = new SqlFile(path);
            Console.WriteLine("Everything worked!!");
            Console.WriteLine("Exit program or click any key to exit...");
            Console.Read();
        }
    }
}

I am using OpenStudio 1.8.0.

A few key things, you need to make sure to build this C# project either as x86 or x64 depending on which version of OpenStudio you installed on your machine (32 bit or 64 bit). You can not use the default "Any CPU" for your C# project.

Also you need to have C:\Program Files\OpenStudio 1.8.0\bin;C:\Program Files\OpenStudio 1.8.0\CSharp\openstudio; in your Path in System Variables. This is needed so your C# program can find the various DLLs for OpenStudio and OpenStudio's C# bindings.

Next in your C# project, you need to include the OpenStudio.dll into the References. Do this by right clicking on "References", click "Add Reference...", click the "Browse..." button at the bottom, then navigate to C:\Program Files\OpenStudio 1.8.0\CSharp\openstudio, and "Add" the OpenStudio.dll file.

Now the example code above should work.

If all of this still doesn't work then you may have a permissions issue with the location of the SQL file.

edit flag offensive delete link more

Comments

Awesome answer Mark! Note that Mark escaped all the backslashes in the path string as well, this is necessary so C# doesn't interpret them as special characters:

http://stackoverflow.com/questions/13...

macumber's avatar macumber  ( 2015-07-31 09:52:23 -0600 )edit

I'm using OpenStudio 1.12.0 with EnergyPlusV8-5-0. There is no .sql file within Examples folder. Hence, I use one of .idf files to generate a, .sql file. Although a simple simulation model runs and generates outputs -including an .sql-file, code in the above returns the following exception : (see the next comment)

cemal's avatar cemal  ( 2016-09-23 04:17:43 -0600 )edit

An unhandled exception of type 'System.TypeInitializationException' occurred in OpenStudio.dll

Additional information: The type initializer for 'OpenStudio.OpenStudioUtilitiesCorePINVOKE' threw an exception.

Any idea to fix this exception ?

An unhandled exception of type 'System.TypeInitializationException' occurred in OpenStudio.dll

cemal's avatar cemal  ( 2016-09-23 04:18:01 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

Stats

Asked: 2015-07-31 01:26:21 -0600

Seen: 452 times

Last updated: Nov 07 '15