Loading

Upload binary data the Database

Submitted by: 


Some times we have the requirement to upload an image to the database in binary form. As uploading a image file to the database has many different benefits as compare to uploading files to the Web Server Folders
So let’s starts from the database table design

The fields includes in table are:
1) ID – Primary Key
2) Filename – Store name of the file.
3) Mime- what type of file is it.
4) Date- date and time of file uploaded
5) Binarydata- it will store the data in binary form

  1. /****** Object: Table [dbo].[BinaryData] Script Date: 02/28/2009 10:47:06 ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. SET ANSI_PADDING ON
  7. GO
  8. CREATE TABLE [dbo].[BinaryData](
  9. [ID] [bigint] IDENTITY(1,1) NOT NULL,
  10. [filename] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
  11. [mime] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
  12. [date] [datetime] NULL,
  13. [binarydata] [varbinary](max) NULL
  14. ) ON [PRIMARY]
  15.  
  16. GO
  17. SET ANSI_PADDING OFF

And next create a new website in Visual Studio IDE name it “binary-data-upload”, by default it will add a “default.aspx” file .In this add the following controls

1) Literal -lit_Status
2) TextBox - FileName
3) FileUpload - FileToUpload1
4) Button - btn_Upload

Double click on the button on the default.aspx
Add the following codes to that particular method

  1. protected void btn_Upload_Click(object sender, EventArgs e)
  2. {
  3. if (FileToUpload1.PostedFile == null || string.IsNullOrEmpty(FileToUpload1.PostedFile.FileName) || FileToUpload1.PostedFile.InputStream == null)
  4. {
  5. lit_Status.Text = "<b>Error!!!</b>";
  6. return;
  7. }
  8. else
  9. {
  10. //lblMsg.Text = "";
  11. string extension = Path.GetExtension(FileToUpload1.PostedFile.FileName).ToLower();
  12. // Response.Write(extension);
  13. string filetype = null;
  14. switch (extension)
  15. {
  16. case ".gif":
  17. filetype = "image/gif";
  18. break;
  19.  
  20. case ".jpg":
  21. case ".jpeg":
  22. case "jpe":
  23. filetype = "image/jpeg";
  24. break;
  25.  
  26. case ".png":
  27. filetype = "image/png";
  28. break;
  29.  
  30. default:
  31. lit_Status.Text = "<b>Invalid Extension</b>";
  32. return;
  33. }
  34. using(SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Strcon"].ConnectionString))
  35. {
  36. try{
  37. const string strSQL="insert into dbo.BinaryData(filename, mime, date, binarydata) values(@filename, @mime, @date, @binarydata)";
  38. SqlCommand cmd=new SqlCommand(strSQL,con);
  39. cmd.Parameters.AddWithValue("@filename",FileName.Text);
  40. cmd.Parameters.AddWithValue("@mime", filetype);
  41.  
  42. byte[] imagebytes = new byte[FileToUpload1.PostedFile.InputStream.Length +1];
  43. FileToUpload1.PostedFile.InputStream.Read(imagebytes, 0, imagebytes.Length);
  44.  
  45. cmd.Parameters.AddWithValue("@date",DateTime.Now);
  46. cmd.Parameters.AddWithValue("@binarydata",imagebytes);
  47.  
  48. con.Open();
  49. cmd.ExecuteNonQuery();
  50. con.Close();
  51.  
  52. }
  53. catch
  54. {
  55. con.Close();
  56. }
  57.  
  58. }
  59.  
  60.  
  61.  
  62. }
  63. }

We in this code we are doing these things
1). Check for the file exists
2). What type of extension( select from predefined format)
3). Insert the values into the database.

About the author:

Planet Source Code is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development




Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.