I have a zip file in i.e C:\Test\test.zip ( this file name and location can be dynamic)
I would like to stroe this zip file in SQL server 2008 R2 ( BLOB data Varbinary (max) ) using C#.
How can I do this? Can you show me some examples?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Feb 16, 2014, 2:58 PM EST
using System.IO;
// ..
string filePath = @"C:\Test\test.zip"; // or whatever
using (SqlConnection conn = new SqlConnection("connection string"))
{
using (SqlCommand cmd = new SqlCommand())
{
byte[] bytes = File.ReadAllBytes(filePath);
cmd.CommandText = @"INSERT INTO SomeTable(id, [file]) VALUES (@id, @file)";
cmd.Parameters.AddWithValue("@id", someId); // some way of identifying file
cmd.Parameters.AddWithValue("@file", bytes);
cmd.Connection = conn;
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
j c45 reputationPosted Feb 19, 2014, 1:21 PM EST
I have created a zip file size about 1.3 GB and I was trying to insert it into SQL server 2008
Varbinary column (MAX) column... but somereason , after the insert is done, the actual data size in database returns 0 ( empty data).. I am not sure what is wrong?
VulpesRank #55 · 29.2K reputationPosted Feb 19, 2014, 1:53 PM EST
If the data size in the database is being returned as 0, then what might be happening is that SQL Server is storing the data as a FILESTREAM because of its size i.e. it's storing it in the file system itself rather than in a table.
You should be doing that anyway if the file is more than 1 MB. See:
http://technet.microsoft.com/en-us/library/bb933993(v=sql.105).aspx
Is there any particular reason why you want to store such a large file in the database rather than just its file path?