我确信这个问题已经得到解答,但是我无法使用搜索工具找到答案。
使用 C# 我想运行一个 .sql 文件。 sql 文件包含多个 sql 语句,其中一些语句分为多行。我尝试读取该文件并尝试使用 ODP.NET 执行该文件...但是我认为 ExecuteNonQuery 并不是真正设计用于执行此操作。
因此,我尝试通过生成一个进程来使用 sqlplus ...但是,除非我将 UseShellExecute 设置为 true 来生成该进程,否则 sqlplus 将挂起并且永远不会退出。这是不起作用的代码。
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "sqlplus";
p.StartInfo.Arguments = string.Format("xx/xx@{0} @{1}", in_database, s);
p.StartInfo.CreateNoWindow = true;
bool started = p.Start();
p.WaitForExit();
WaitForExit 永远不会返回....除非我将 UseShellExecute 设置为 true。 UseShellExecute 的副作用是您无法捕获重定向的输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using System.Data.SqlClient;
public partial class ExcuteScript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sqlConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ccwebgrity;Data Source=SURAJIT\SQLEXPRESS";
string script = File.ReadAllText(@"E:\Project Docs\MX462-PD\MX756_ModMappings1.sql");
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)