问题描述
不要以为任何人都知道如何从nhibernate执行sql脚本.我在staticData.sql文件中包含了数据库中需要的一些静态数据.当我运行集成测试时,我使用schema export命令重新创建数据库,并且需要在其中运行这些数据.我意识到我可以使用.net来获取它,但是我真的很想在我的数据库中使用一种数据访问技术.项目...
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...
谢谢
推荐答案
尝试使用NHibernate.ISession.CreateSQLQuery(string queryString)
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();
}
}
这里是文档:第16章. SQL
这篇关于Nhibernate和SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!