回答

收藏

执行存储作为资源SQL脚本

技术问答 技术问答 97 人阅读 | 0 人回复 | 2023-09-13

我想在解决方案中存储冗长的.sql脚本以编程的方式执行。我已经想出了如何执行包括我在内的内容。sql脚本字符串,但还没有想出如何从存储在解决方案中的文件中读取字符串(例如/$ F$ p) f$ u" U5 L
Scripts夹下子文件)。0 s* Z, C9 l% a9 @5 P/ h9 ~
                                                                % Z* k- i& b+ T1 U7 @5 K$ g$ x' y
    解决方案:                                                               
$ d* ]% u7 H# _; ~) p# T                                                                首先,编辑.sql文件的属性,以便将其作为资源嵌入。  Q; e1 |2 T9 `. C0 [6 K
然后用类似以下代码的代码检索脚本:9 n$ |: s7 S7 m$ Z1 E1 v# o
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();
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则