2010-08-16 30 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