C#中:执行作为资源存储在SQL脚本 [英] C#: Executing a SQL script stored as a resource

查看:116
本文介绍了C#中:执行作为资源存储在SQL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题:我想存储漫长的.sql脚本在我的解决方案,并通过编程执行它们。我已经找到了如何执行包含我的SQL脚本一个字​​符串,但我还没有想出如何读取,将存储在解决方案文件中的字符串(下/脚本子文件夹为例)。

Newbie question : I would like to store lengthy .sql scripts in my solution and execute them programmatically. I've already figured out how to execute a string containing my sql script but I haven't figured out how to read the string from a file that would be stored in the solution (under a /Scripts subfolder for example).

非常感谢,

推荐答案

首先,这样它会被作为资源嵌入编辑.sql文件的属性。

First, edit the .sql file's properties so that it will be embedded as a resource.

然后用code类似于以下检索脚本:

Then use code similar to the following to retrieve the script:

string commandText;
Assembly thisAssembly = Assembly.GetExecutingAssembly();
using (Stream s = thisAssembly.GetManifestResourceStream(
      "{project default namespace}.{path in project}.{filename}.sql"))
{
   using (StreamReader sr = new StreamReader(s))
   {
      commandText = sr.ReadToEnd();
   }
}

这篇关于C#中:执行作为资源存储在SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆