如何将mp3作为字节存储在SQL Server中 [英] How to store mp3 as bytes in sql server

查看:127
本文介绍了如何将mp3作为字节存储在SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何读写MP3到数据库

Possible Duplicate:
how to read and write MP3 to database

我需要将mp3文件读取并存储为sql服务器和C#中的字节.我该怎么办?

Hi I need to read and store mp3 files as bytes in sql server and C#.. How can I do that?

推荐答案

SQL Server BLOB(二进制大对象)使您可以将图像和mp3数据作为原始二进制存储在数据库中.如果您想快速启动并运行某项功能,请选中此选项.

SQL Server BLOB (Binary Large Object) lets you store image and mp3 data as raw binary in database. if you want get something up and running quickly, check this one.

http://www.databasejournal.com/features/mssql/article.php/3724556/Storing-Images-and-BLOB-files-in-SQL-Server-Part-2.htm

使用ADO,您可以打开文件流到mp3,然后将类型为SQLDbType.VarBinary的命名参数(SQLParameter)添加到SQLCommand对象

Using ADO, you can open a file stream to your mp3 and then add a named parameter (SQLParameter) of type SQLDbType.VarBinary to your SQLCommand object

SqlConnection conn = new SqlConnection(yourConnectionString);

SqlCommand cmd = null;
SqlParameter param = null;

cmd = new SqlCommand("INSERT INTO MP3 SET DATA = @BLOBPARAM WHERE
'some criteria'", conn);

FileStream fs = null;
fs = new FileStream("your mp3 file", FileMode.Open, FileAccess.Read);
Byte[] blob = new Byte[fs.Length];
fs.Read(blob, 0, blob.Length);
fs.Close();

param = new SqlParameter("@BLOBPARAM", SqlDbType.VarBinary, blob.Length,
ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();

这篇关于如何将mp3作为字节存储在SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆