Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SQL server FILETABLE检索到的文件已损坏_C#_Sql Server - Fatal编程技术网

C# 从SQL server FILETABLE检索到的文件已损坏

C# 从SQL server FILETABLE检索到的文件已损坏,c#,sql-server,C#,Sql Server,我在SQL Server 2014上创建了一个FILETABLE,其中包含所有默认选项,用于检索数据和插入和删除文件的存储过程。请注意,数据库上未启用非事务性访问,因为文件在文件系统上不应可见 问题是,当我从表中检索一个文件并将其写入磁盘时,它与原始文件不同,并且不能用相关程序打开 视图的代码: SELECT stream_id, file_stream, name, CAST(creation_time AS DateTime) AS DateCreated, CAST(last_write_

我在SQL Server 2014上创建了一个FILETABLE,其中包含所有默认选项,用于检索数据和插入和删除文件的存储过程。请注意,数据库上未启用非事务性访问,因为文件在文件系统上不应可见

问题是,当我从表中检索一个文件并将其写入磁盘时,它与原始文件不同,并且不能用相关程序打开

视图的代码:

SELECT stream_id, file_stream, name, CAST(creation_time AS DateTime) AS DateCreated, CAST(last_write_time AS DateTime) AS DateUpdated FROM dbo.DocumentFiles
插入过程的代码:

create PROCEDURE [dbo].[sp_DocumentFiles_create]
-- Add the parameters for the stored procedure here
@name nvarchar(255),
@data varbinary(max)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

-- Insert statements for procedure here
DECLARE @id uniqueidentifier;
set @id = NEWID();
insert into DocumentFiles (stream_id, file_stream, name) VALUES(@id, @data, @name);
select @id;
END
用于读取的代码:

DocumentFile result = null;
            SqlCommand comm = new SqlCommand("SELECT * from DocumentView where stream_id = @id", conn);
            comm.Parameters.Add(new SqlParameter("@id", id));
            try
            {
                conn.Open();
                SqlDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    result = new DocumentFile();
                    result.stream_id = (Guid)reader["stream_id"];
                    result.file_stream = (byte[])reader["file_stream"];
                    result.name = reader["name"].ToString();
                    result.DateCreated = Convert.ToDateTime(reader["DateCreated"]);
                    result.DateUpdated = Convert.ToDateTime(reader["DateUpdated"]);
                }
            }
            catch
            {

            }
            finally
            {
                conn.Close();
            }
            return result;
用于插入的代码:

public Guid? Insert(DocumentFile obj)
        {
            Guid? result = null;
            SqlCommand comm = new SqlCommand("sp_DocumentFiles_create", conn);
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@name", obj.name));
            comm.Parameters.Add(new SqlParameter("@data", obj.file_stream));
            try
            {
                conn.Open();
                result = comm.ExecuteScalar() as Guid?;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

我唯一能够插入和检索完整的文件是图像文件。我尝试使用各种ms office文档(.doc、.docx、.pptx等)以及归档文件(rar、zip)。

在禁用异常处理的情况下运行可能会很有见地,在十六进制编辑器中查看输入/输出文件之间的内容差异也会很有见地。稍微绕道一下……在继续使用sp_uuu前缀之前,您可能需要阅读这些内容。您还应该将您的命令和连接对象包装在USING语句中,以便始终正确处理它们。感谢您的回答和建议。我在一个十六进制编辑器中检查了这些文件,我看到了它们之间的差异,但我不知道如何解释它们。