繁体   English   中英

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

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

我能够将视频文件二进制数据拆分并保存到 SQL 服务器中,整个视频大小为 14 MB; 但从数据库下载时只有 13MB,视频无法播放。

我认为我的连接代码不正确。 你能帮忙的话,我会很高兴。

拆分并保存代码:

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();
}

连接代码:

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();

您的 select 查询中没有指定排序,这些部分可能 select 的顺序错误。 请创建一个标识列以保留插入到表中的部分序列。 然后在 select 中查询 order rows ASC by the identity column。 请这样做并检查我是否正确。

暂无
暂无

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

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