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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Feb 16, 2014, 7:58 PM
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);
}
}
}
VulpesPosted Feb 19, 2014, 6:53 PM
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?
j cPosted Feb 19, 2014, 6:21 PM
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?