如何使用 BCP 写入远程 SQL 服务器?

How to write using BCP to a remote SQL Server?

我有一个远程 SQL 服务器,其主机名是我用来连接的。

BCP 建议使用

bcp DBName.dbo.tablename in C:\test\yourfile.txt -c -T -t

但是,当我尝试这样做时,它没有连接到 DBName,因为它不是有效的别名。我收到本机错误 2.

如何 运行 BCP 但指定要连接的互联网/网络地址,而不是 MSSQL 服务器名称?

DBName为数据库名称,需要使用-S选项提供服务器(或server\instance)名称。

来自MSDN

-S server_name [\instance_name] Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility connects to the default instance of SQL Server on the local computer. This option is required when a bcp command is run from a remote computer on the network or a local named instance. To connect to the default instance of SQL Server on a server, specify only server_name. To connect to a named instance of SQL Server, specify server_name\instance_name.

How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?

您可以指定 IP 地址(这里只是 127.0.0.1)而不是服务器名称。

bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1\instance"

如果你真的想去 IP\port 只有用逗号分隔的 IP 地址和端口

bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1,60905"

当然,在那种情况下,您需要知道实例正在使用的端口,并且您的实例应该设置为使用固定端口