2010-08-16 29 views
0

可能重复:
how to read and write MP3 to database如何存储的MP3在SQL服务器的字节

您好我需要读取和存储的MP3文件,在SQL Server和C#字节。如何能我这样做?

+0

是关于C#的问题或专门的SQL? 您没有指定SQL Server的版本,@ Nix的答案与sql server 2008相关,但在此之前,您可能受限于常规BLOB。 – 2010-08-16 13:15:10

+0

我应该在我的帖子中告诫,但你能解释一下你想如何访问你的数据库吗? – Nix 2010-08-16 13:17:29

回答

0

看看使用FileStream,看看这article,因为它解释什么时候,为什么,以及如何使用文件流。

为了让我用C#来帮助你,请让我知道你打算如何连接和访问你的数据库。

0

SQL Server BLOB(二进制大对象)允许您将图像和mp3数据作为原始二进制文件存储在数据库中。如果您想快速启动并运行,请选中此项。

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对象

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

嗨我试过我的代码,它工作正常,当我手动输入文件名,但我想从文件上传控制得到它,我该怎么做? – Leema 2010-08-19 10:38:56

+0

最简单的方法是暂时缓冲mp3文件,然后使用上面的代码。 – 2010-08-22 18:56:56