How to store mp3 as bytes in sql server [duplicate]

ぃ、小莉子 提交于 2020-01-03 02:41:07

问题


Possible Duplicate:
how to read and write MP3 to database

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


回答1:


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

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



回答2:


Look into using FileStream, check out this article because it explains when, why, and how to use filestream.

In order for me to help you with C# please let me know how you plan on connecting and accessing your database.



来源:https://stackoverflow.com/questions/3493387/how-to-store-mp3-as-bytes-in-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!