Nhibernate and sql scripts

Don't think anyone knows how to execute sql script from nhibernate. Ive got some static data that I need in the database, which is contained in the staticData.sql file. When I run my integration tests, I recreate the database using the export schema command, and I need to run this data. I understand that I can use it .net, but I really like to have only one data access technique in my project ...

thanks

+5
source share
2 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

+13
source

ADO.NET . , NHibernate. , , , .

+4

All Articles