How to store mp3 as bytes in sql server [duplicate

2019-08-22 15:14发布

问题:

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:

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.



回答2:

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


标签: c# .net mp3 byte