简体   繁体   English

“sqlcmd.exe -S Instance USE ...”在命令行中工作,但我无法将其移动到 c # process

[英]"sqlcmd.exe -S Instance USE ..." work in command line, but I can't move it to c # process

I am trying move this command to C# Process:我正在尝试将此命令移至 C# 过程:

SQLCMD.EXE -S InstanceName
USE [master]
GO
CREATE DATABASE [Ek] ON
( FILENAME = N'C:\Program Files\Microsoft SQL 
Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Ek_Primary.mdf' ),
( FILENAME = N'C:\Program Files\Microsoft SQL 
Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Ek_Primary.ldf' )
FOR ATTACH ;
GO
EXIT

I have:我有:

try
{
     Process p = CreateProcess();
     p.StartInfo.FileName = @"C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE";
     p.StartInfo.Arguments = "-S InstanceName" + "\n" +
     "USE [master]" + "\n" +
     "GO" + "\n" +
     "CREATE DATABASE [Ek] ON" + "\n" +
     "( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\Ek_Primary.mdf' )," + "\n" +
     "( FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\MSSQL\\DATA\\Ek_Primary.ldf' )" + "\n" +
     "FOR ATTACH ;" + "\n" +
     "GO" + "\n" +
     "EXIT" + "\n";
     Console.WriteLine(p.StartInfo.Arguments);
     p.Start();
     var output = p.StandardOutput.ReadToEnd();
     var err = p.StandardError.ReadToEnd();
     Console.WriteLine("O: " + output);
     Console.WriteLine("E: " + err);
}
catch (Exception e) { Console.WriteLine(e.Message); ; }

It return err = Unexpected argument.它返回 err = Unexpected 参数。 Enter '-?'进入 '-?' for help.求助。

I was trying set FileName on cmd.exe and move path to Arguments. But it waits forever for a response and does not exit p.StandardOutput.ReadToEnd ();我正在尝试在 cmd.exe 上设置文件名并将路径移动到 Arguments。但它会永远等待响应并且不会退出 p.StandardOutput.ReadToEnd ();

I was trying send each line of code individually, but also without success.我试图单独发送每一行代码,但也没有成功。

And I trying with /C on start p.StartInfo.Argument but it does not change anything.我在开始 p.StartInfo.Argument 时尝试使用 /C,但它没有任何改变。

Thanks to Selvin suggestion.感谢塞尔文的建议。 I made it by:我做到了:

string sqlConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Data Source=InstanceName";
string script = File.ReadAllText(@"../../sql.sql");
Microsoft.Data.SqlClient.SqlConnection conn = new Microsoft.Data.SqlClient.SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);

where in sql.sql I added all my sql line.在 sql.sql 中,我添加了所有 sql 行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以配置sqlcmd.exe路径吗? - Can sqlcmd.exe path be configured? 可以将相同的sqlcmd.exe用于任何数据库备份 - Can the same sqlcmd.exe used for any db back up 检测是否安装了 SQLCMD.exe 的正确方法? - Proper way to detect if SQLCMD.exe is installed? 如何使用 c# 运行 exe 文件(我不能使用 Process.Start() 因为我不知道 exe 文件的位置) - How can I run an exe file with c# (I can't use Process.Start() because I don't know the exe file's location) 我可以捕获用C#的System.Diagnostic.Process()启动的命令行应用程序的APPCRASH吗? - Can I catch APPCRASH of command line app launched with C#'s System.Diagnostic.Process()? 命令行和exe的 - Command line and exe's 带有退出事件的startet时,无法在C#中启动sqlcmd进程 - Starting sqlcmd process in C# doesn't work when startet with exited event VS2012 link.exe是从命令行运行的,而不是从C#的System.Diagnostics.Process运行的? - VS2012 link.exe runs from command line, but not from C#'s System.Diagnostics.Process? 当我从 C# 创建进程时 psexec.exe 不起作用 - psexec.exe doesn't work when I create a Process from C# 如何使用安装程序使cmd.exe中的ac#命令行应用程序可运行? - How can I make a c# command line application runnable in cmd.exe with installer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM