Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate and sql scripts

Tags:

nhibernate

Dont suppose anyone knows how to execute a sql script from nhibernate. Ive got some static data that I need in a database, that is contained, in a staticData.sql file. When I run my integration tests, I recreate the database with the schema export command, and I need to run this data in. I realise I can get it in using .net, but Id really like to only have one data access technique in my project...

Thanks

like image 342
user344836 Avatar asked Sep 14 '09 08:09

user344836


People also ask

What is NHibernate in C#?

NHibernate is a mature, open source object-relational mapper for the . NET framework. It's actively developed, fully featured and used in thousands of successful projects. Easily map regular C# or VB.NET object models designed in Visual Studio.

How do I install NHibernate Profiler?

You can easily install the NHibernate Profiler into your application from NuGet. Let's go to the NuGet Manager console from the Tools menu by selecting the NuGet Package Manager → Package Manager Console. It will open the Package Manager Console window. Enter the following command and press enter.


1 Answers

Try using NHibernate.ISession.CreateSQLQuery(string queryString)

    private NHibernate.ISession session; // Initialized somewhere

    public void ExecuteSQLFile(string sqlFilePath)
    {
        string sqlScript;

        using (FileStream strm = File.OpenRead(sqlFilePath))
        {
            var reader = new StreamReader(strm);
            sqlScript = reader.ReadToEnd();
        }

        var regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        string[] lines = regex.Split(sqlScript);

        foreach (string line in lines)
        {
            IQuery query = session.CreateSQLQuery(line);
            query.ExecuteUpdate();
        }
    }

Here is the documentation: Chapter 16. Native SQL

like image 70
Lars Udengaard Avatar answered Oct 18 '22 04:10

Lars Udengaard