我正在批量复制到我的数据库中的select文件中。
DECLARE @cmd varchar(1000)
DECLARE @sql varchar(8000)
SET @cmd='"select * from [MyDB].[dbo].MyTable"'
SELECT @sql = 'bcp '+@cmd+' queryout C:\myfile.txt -c -t -T -S MyServer -U user -P password';
exec xp_cmdshell @sql;
如果我更改参数并在我的机器上执行相同的命令,那么它可以工作,但在数据库服务器上我收到此错误:
Msg 121, Level 20, State 0, Line 0
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.)
我检查服务器名称,用户,密码,表名是否正确,所以我无法理解我做错了什么。 有人可以帮我解决这个问题吗? 感谢
答案 0 :(得分:0)
增加连接字符串中的超时秒数。
private static void OpenSqlConnection() {
string connectionString = GetConnectionString();
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionTimeout: {0}",
connection.ConnectionTimeout);
}
}
static private string GetConnectionString() {
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source=(local);Initial Catalog=AdventureWorks;" + "Integrated Security=SSPI;Connection Timeout=30";
}

答案 1 :(得分:0)
我有同样的错误。第三方工具连接到数据库以提取数据以导入商业智能系统。每次提取/导入将运行大约 1 小时,并且在此过程中会有大约 10 到 15 个单独的查询。幸运的是,我们已经登录了——所以我们知道每个查询的开始和结束时间。提取/导入过程说它在 30 分钟后成功完成 - 而不是大约需要 1 小时。我能够隔离过程失败的查询。当我在 SSMS 中运行该查询时,我遇到了您在问题中给出的相同错误。
但是,当我在另一个环境中运行该查询时,我收到一个错误,即子查询不能返回多于一行。
果然,当我在 prod 环境中注释掉我的子查询时,查询运行没有任何错误。
所以最终,我的根本原因是一个返回多行的子查询 - 并且这个问题只是因为某种原因出现,“坏数据”进入了数据库 - 即。那个特定的子查询不应该发现有不止一行的场景,因此,错误也开始出现在某一天。 (对于间歇性遇到此错误的其他人 - 可能是因为只有您的部分查询 - 或您的一项查询 - 失败)。