There are a lot of things to consider here.
- Actual Database Server, is it SQL Server, Oracle etc.
- Are you planning on storing the file IN the database, or storing the location and leaving the file on the filesystem somewhere?
- If you are storing the files IN the database, how large is your database allowed to be, how large of files can your database hold, etc.
I have seen proof that certain databases are faster at retrieving files than the filesystem. Additionally you don't have to worry about accidental removal of a file or corruption of a file on the filesystem if you store it in the database. Finally an additional benefit to storing the files in the database is, you don't have to worry about filesystem permissions.
Storage limitations of the database is the greatest drawback of storing the images IN the database.
Here is an article from Oracle on doing this.
Assuming you want to stuff files into a database fits your needs, you can use a connected dataset, a O/R mapper (like nhibernate) or you can do it simply with parameterized SQL.
var css = ConfigurationManager.ConnectionStrings["DSN"];
byte[] fileData = System.IO.File.ReadAllBytes("filename.flv");
var factory = DbProviderFactories.GetFactory(css.ProviderName);
using (var conn = factory.CreateConnection())
{
var csb = factory.CreateConnectionStringBuilder() ?? new DbConnectionStringBuilder(true);
csb.ConnectionString = css.ConnectionString;
conn.ConnectionString = csb.ConnectionString;
conn.Open();
using (var cmd = conn.CreateCommand())
{
var parameter = factory.CreateParameter();
parameter.DbType = DbType.Binary;
parameter.ParameterName = "id";
parameter.Value = Guid.NewGuid().ToByteArray();
cmd.Parameters.Add(parameter);
parameter = factory.CreateParameter();
parameter.DbType = DbType.String;
parameter.ParameterName = "name";
parameter.Value = "filename";
cmd.Parameters.Add(parameter);
parameter = factory.CreateParameter();
parameter.DbType = DbType.String;
parameter.ParameterName = "name";
parameter.Value = fileData;
cmd.Parameters.Add(parameter);
cmd.CommandText = @"insert into `Test`.`imagetable`
(`ID`, `Name`, `data`) values
(?id, ?name ,?data)";
}
}
The SQL in this example is hardcoded to use MySQL (but its ONLY the SQL that forces it to be mySQL). You can see how to make this more database independent in some of my articles.
Don't hard code your DataProviders
Using Information from the .NET DataProvider