简体   繁体   English

如何连接 SQL 服务器中保存的视频文件的字节数组?

[英]How to Concatenate Byte Array of a Video File saved in SQL Server?

I was able to split video file binary data and save into SQL Server, the entire video size is 14 MB;我能够将视频文件二进制数据拆分并保存到 SQL 服务器中,整个视频大小为 14 MB; But while downloading from Database its only 13MB and video is not playable.但从数据库下载时只有 13MB,视频无法播放。

I think my concatenation code is not correct.我认为我的连接代码不正确。 Any help would be nice.你能帮忙的话,我会很高兴。

Split and save code:拆分并保存代码:

FileStream fs = null;
fs = new FileStream("test.mp4", FileMode.Open, FileAccess.Read);

Byte[] blob = new Byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();

byte[] smallPortion;

for (int i = 1800000; i < blob.Length; i = i + 1800000) 
{
    smallPortion = blob.Skip(i).Take(1800000).ToArray();

    cmd = new SqlCommand("INSERT INTO machineInfo (machineFile) values (@BLOBPARAM) ", conn);
    param = new SqlParameter("@BLOBPARAM", SqlDbType.VarBinary, blob.Length,ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, smallPortion); 
    cmd.Parameters.Add(param);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

Concatenate code:连接代码:

SqlCommand command = new SqlCommand("select machineFile  from machineInfo", conn);

// Read Start
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

byte[] productImage = new byte[0];
byte[] productImage2 = new byte[0];

foreach (DataRow dr in dt.Rows)
{
    productImage = (byte[])dr["machineFile"];
    productImage2 = productImage2.Concat(productImage).ToArray(); 
}            

byte[] buffer = productImage2; 
conn.Close();

FileStream fs = new FileStream(@"H:\test.mp4", FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();

There is no sorting specified in your select query, and those parts may select in a wrong order.您的 select 查询中没有指定排序,这些部分可能 select 的顺序错误。 Please create an identity column to preserve the sequence of parts which are inserted to the table.请创建一个标识列以保留插入到表中的部分序列。 Then in select query order rows ASC by the identity column.然后在 select 中查询 order rows ASC by the identity column。 Please do this and check I am correct or not.请这样做并检查我是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM