Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Upload binary data the Database
planetsourcecode's picture
Language: .NET
Category: Databases

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

0
Your rating: None



Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...