C#.Net SQL Server 2012-插入文件-System.Data.SqlClient.SqlException
我正在尝试将文件插入sql文件流。我在测试中使用的文件只有87 KB 我有一个SQL Server 2012 Enterprise实例运行在Windows 2012 R2服务器和web服务器(2012 R2)上。在web上运行的应用程序每分钟可通过SQL Server提供数百个事务,没有任何问题 每次尝试执行接受C#.Net SQL Server 2012-插入文件-System.Data.SqlClient.SqlException,c#,sql,.net,windows-server-2012-r2,sqlexception,C#,Sql,.net,Windows Server 2012 R2,Sqlexception,我正在尝试将文件插入sql文件流。我在测试中使用的文件只有87 KB 我有一个SQL Server 2012 Enterprise实例运行在Windows 2012 R2服务器和web服务器(2012 R2)上。在web上运行的应用程序每分钟可通过SQL Server提供数百个事务,没有任何问题 每次尝试执行接受varbinary(max)参数的存储过程时,都会出现以下错误: System.Data.SqlClient.SqlException:从服务器接收结果时发生传输级别错误。(提供程序:T
varbinary(max)
参数的存储过程时,都会出现以下错误:
System.Data.SqlClient.SqlException:从服务器接收结果时发生传输级别错误。(提供程序:TCP提供程序,错误:0-远程主机已强制关闭现有连接。)
System.ComponentModel.Win32Exception:远程主机强制关闭了现有连接
当我从存储过程中删除到varbinary(max)
时,我能够避免错误
这就是为什么我可以得出结论,这就是我的问题所在
尝试将varbinary
转换为base64varchar(max)
代码示例-这是同步调用存储过程的尝试
//
//This is an attempt to call the stored procedure synchronously.
public static void InsertFile1(string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
try
{
SqlConnection conn = null;
SqlDataReader rdr = null;
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("SPROC", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ProjectKey", SqlDbType.VarChar, 30).Value = ProjectKey;
cmd.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = FileName;
cmd.Parameters.Add("@ClientID", SqlDbType.VarChar, 300).Value = ClientID;
cmd.Parameters.Add("@FileType", SqlDbType.VarChar, 300).Value = FileType;
cmd.Parameters.Add("@File", SqlDbType.VarBinary, -1).Value = File;
cmd.Parameters.Add("@FileCategoryId", SqlDbType.Int, 5).Value = FileCategoryId;
rdr = cmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception("InsertClientFileVoid3", ex);
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
catch (Exception ex)
{
throw new Exception("InsertClientFile", ex);
}
}
//
//This is an attempt to call the stored procedure asynchronously.
public static void InsertFile2(string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
try
{
E2EStream(CancellationToken.None, ProjectKey, FileName, ClientID, FileType, File, FileCategoryId).Wait();
}
catch (Exception ex)
{
throw new Exception("InsertFile", ex);
}
}
//
//Function that does the async call to SQL Server
private static async Task E2EStream(CancellationToken cancellationToken, string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
using (SqlConnection writeConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETLWarehouseSalesforce"].ToString()))
{
Task openWriteConn = writeConn.OpenAsync(cancellationToken);
await Task.WhenAll(openWriteConn);
using (SqlCommand writeCmd = new SqlCommand("[Publishing].[Insert_File]", writeConn))
{
writeCmd.CommandType = CommandType.StoredProcedure;
writeCmd.Parameters.Add("@ProjectKey", SqlDbType.VarChar, 30).Value = ProjectKey;
writeCmd.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = FileName;
writeCmd.Parameters.Add("@ClientID", SqlDbType.VarChar, 300).Value = ClientID;
writeCmd.Parameters.Add("@FileType", SqlDbType.VarChar, 300).Value = FileType;
writeCmd.Parameters.Add("@File", SqlDbType.VarBinary, -1).Value = File;
writeCmd.Parameters.Add("@FileCategoryId", SqlDbType.Int, 5).Value = FileCategoryId;
await writeCmd.ExecuteNonQueryAsync(cancellationToken);
}
}
}
这是一种异步调用存储过程的尝试
//
//This is an attempt to call the stored procedure synchronously.
public static void InsertFile1(string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
try
{
SqlConnection conn = null;
SqlDataReader rdr = null;
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("SPROC", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ProjectKey", SqlDbType.VarChar, 30).Value = ProjectKey;
cmd.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = FileName;
cmd.Parameters.Add("@ClientID", SqlDbType.VarChar, 300).Value = ClientID;
cmd.Parameters.Add("@FileType", SqlDbType.VarChar, 300).Value = FileType;
cmd.Parameters.Add("@File", SqlDbType.VarBinary, -1).Value = File;
cmd.Parameters.Add("@FileCategoryId", SqlDbType.Int, 5).Value = FileCategoryId;
rdr = cmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception("InsertClientFileVoid3", ex);
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
catch (Exception ex)
{
throw new Exception("InsertClientFile", ex);
}
}
//
//This is an attempt to call the stored procedure asynchronously.
public static void InsertFile2(string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
try
{
E2EStream(CancellationToken.None, ProjectKey, FileName, ClientID, FileType, File, FileCategoryId).Wait();
}
catch (Exception ex)
{
throw new Exception("InsertFile", ex);
}
}
//
//Function that does the async call to SQL Server
private static async Task E2EStream(CancellationToken cancellationToken, string ProjectKey, string FileName, string ClientID, string FileType, Byte[] File, int FileCategoryId)
{
using (SqlConnection writeConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETLWarehouseSalesforce"].ToString()))
{
Task openWriteConn = writeConn.OpenAsync(cancellationToken);
await Task.WhenAll(openWriteConn);
using (SqlCommand writeCmd = new SqlCommand("[Publishing].[Insert_File]", writeConn))
{
writeCmd.CommandType = CommandType.StoredProcedure;
writeCmd.Parameters.Add("@ProjectKey", SqlDbType.VarChar, 30).Value = ProjectKey;
writeCmd.Parameters.Add("@FileName", SqlDbType.VarChar, 300).Value = FileName;
writeCmd.Parameters.Add("@ClientID", SqlDbType.VarChar, 300).Value = ClientID;
writeCmd.Parameters.Add("@FileType", SqlDbType.VarChar, 300).Value = FileType;
writeCmd.Parameters.Add("@File", SqlDbType.VarBinary, -1).Value = File;
writeCmd.Parameters.Add("@FileCategoryId", SqlDbType.Int, 5).Value = FileCategoryId;
await writeCmd.ExecuteNonQueryAsync(cancellationToken);
}
}
}
这是从C调用的存储过程#
调用此版本的过程有效
CREATE PROCEDURE [dbo].[InsertFile]
@ProjectKey varchar(30) = ''
,@FileName varchar(300) = ''
,@ClientID varchar(300) = ''
,@FileType varchar(300) = ''
--,@File varbinary(MAX) = null
,@FileCategoryId int = 1
AS
BEGIN
print 'Stuff will happen here';
END
在DB-A上绕过存储过程
USE [DB-A]
ALTER PROCEDURE [Publishing].[Insert_File]
@ProjectKey varchar(30) = ''
,@FileName varchar(300) = ''
,@ClientID varchar(300) = ''
,@FileType varchar(300) = ''
,@File varbinary(MAX) = null
,@FileCategoryId int = 1
AS
BEGIN
EXEC [DB-B].[Publishing].[Insert_File] @ProjectKey, @FileName, @ClientID, @FileType, @File, @FileCategoryId;
END
错误消息:
System.Data.SqlClient.SqlException:从服务器接收结果时发生传输级别错误。
(提供程序:TCP提供程序,错误:0-远程主机已强制关闭现有连接。)
System.ComponentModel.Win32Exception:远程主机强制关闭了现有连接
在异步情况下,您是否忘记将
CommandType
设置为StoredProcedure
,或者我只是错过了它?德克-感谢您的关注。我只是在示例中忽略了这一点。我已经更新了异步snippit。仍在接收错误。已添加信息。。。当我从本地机器进行调试时,它会抛出相同的错误,从而告诉我web服务器上的某些内容不是特定的。此外,如果我将null传递给varbinary参数,它也会工作。这可能与SQL终止其认为是大量数据的连接有关吗?@dirk-感谢您的关注。我只是在示例中忽略了这一点。我已经更新了异步snippit。仍在接收错误。其他信息。。。我有一个工作,但这并不能解决核心问题。在调用DB-B上发生故障的存储过程之前,会与同一服务器的另一个数据库(DB-A)建立多个其他连接。我创建了一个过程DB-a,它将从DB-B调用该过程,这是可行的。如果我尝试在没有其他db连接的情况下调用测试中的原始过程,它会工作(仅连接db-B)。思想?