Nhibernate and sql scripts

后端 未结 2 836
闹比i
闹比i 2021-01-13 05:48

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

2条回答
  •  余生分开走
    2021-01-13 06:23

    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

提交回复
热议问题