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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With