使用Java执行tablediff.exe时参数无效

时间:2013-06-26 06:52:52

标签: java sql-server command-line-arguments

Alrite。所以这就是我到目前为止所拥有的。我发现了一篇关于如何通过Java运行可执行文件的帖子。我使用它,tablediff实用程序正在运行。但是我必须按照SQL Server的要求将参数传递给tablediff实用程序。

代码:

public static void main(String[] args) throws IOException

    {

        String sourceserver = "-sourceserver source_name";
        String sourcedatabase ="-sourcedatabase dbtest01";
        String sourcetable = "-sourcetable article";
        String destinationserver = "-destinationserver destination_name";
        String destinationdatabase ="-destinationdatabase dbtest02";
        String destinationtable = "-destinationtable article";

        Process process = new ProcessBuilder("C:\\Program Files\\Microsoft SQL Server\\110\\COM\\tablediff.exe",sourceserver,sourcedatabase,sourcetable,destinationserver,destinationdatabase,destinationtable).start();    

        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        System.out.printf("Output of running %s is:", Arrays.toString(args));

        while ((line = br.readLine()) != null) 
        {
          System.out.println(line);
        }
    }

我一直收到这个错误:

Output of running [] is:
Microsoft (R) SQL Server Replication Diff Tool 
Copyright (c) 2008 Microsoft Corporation

User-specified agent parameter values:
-sourceserver source_name 
The parameter '-sourceserver source_name' is invalid.

命令提示符执行它:

C:\Program Files\Microsoft SQL Server\110\COM\>tablediff.exe -sourceserver source_name -sourcedatabase dbtest01 -sourcetab
le article -destinationserver destination_name -destinationdatabase dbtest02 -destin
ationtable article

Microsoft (R) SQL Server Replication Diff Tool
Copyright (c) 2008 Microsoft Corporation

User-specified agent parameter values:
-sourceserver source_name
-sourcedatabase dbtest01
-sourcetable article
-destinationserver destination_name
-destinationdatabase dbtest02
-destinationtable article

Table [dbtest01].[dbo].[article] on source_name and Table [dbtest02].[dbo].[art
icle] on destination_name have 1 differences.
Err     id
Dest. Only      N'004       '
The requested operation took 9.1819181 seconds.

我不知道程序为什么会给出无效参数。我尝试将字符串更改为“source_name”等等。不行。我也尝试将字符串直接传递给Processbuilder。它也不起作用。它给了我相同的错误信息。

1 个答案:

答案 0 :(得分:0)

从输入中我会说source_name和destination_name需要是数据库服务器的名称

String sourceserver = "-sourceserver source_name";
String destinationserver = "-destinationserver destination_name"; 

-sourceserver source_server_name [\ instance_name] 是源服务器的名称。 为SQL Server的默认实例指定source_server_name。为SQL Server的命名实例指定source_server_name \ instance_name。

-destinationserver destination_server_name [\ instance_name] 是目标服务器的名称。为SQL Server的默认实例指定destination_server_name。 为SQL Server的命名实例指定destination_server_name \ instance_name。

编辑>>

你能尝试-sourceserver \\ MSSQLSERVER我认为它可能是\并且为单个反斜杠转义,所以你只是得到“\ MSSQLSERVER”