在命令提示符下获取osql输出

时间:2013-08-08 11:00:53

标签: c# sql-server tsql

我使用以下代码执行osql命令,然后获取其输出(如2行受影响等),但它永远不会完成。 请让我知道我错过了什么。

string sqlFilePath = Helper.GetFilePath(sqlFileName, Environment.CurrentDirectory);

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"osql -E -S @Server -d @Database -T -n -i ""@SqlFile"""
           .Replace("@Server", ConfigurationManager.AppSettings["Server"])
           .Replace("@Database", Path.GetFileNameWithoutExtension(DB))
           .Replace("@SqlFile", sqlFilePath);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

1 个答案:

答案 0 :(得分:1)

我相信你可能遇到两个不同的问题:

  • 设置FileNameArguments属性,如下所示:

    p.StartInfo.FileName = "osql.exe";
    p.StartInfo.Arguments = @"-E -S @Server -d @Database -T -n -i ""@SqlFile"""
        .Replace("@Server", ConfigurationManager.AppSettings["Server"])
        .Replace("@Database", Path.GetFileNameWithoutExtension(DB))
        .Replace("@SqlFile", sqlFilePath);
    
  • 您可能还会遇到编码问题。确保使用 Unicode编码(代码页1200)here'描述问题的问题)保存 .sql 文件。

相关问题