Click here to Skip to main content
13,293,894 members (59,583 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to insert photo in sql server 2005 with C# code?
Posted 16-Jan-13 5:07am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

And... what have you tried so far?

There are number of articles on web on how to insert an image into database. Topics/Tools:
1. Use ADO.NET
2. DB table design to have image stored
3. Query

Try out, be specific.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Try this:

1) You need to know everything about a file you a trying to upload
Use this(BellUpload is a asp:FileUpload control name):
FileInfo fi = new FileInfo(BellUpload.FileName);

2) You need a memory buffer:
Byte[] buffer = new Byte[BellUpload.PostedFile.ContentLength];

3) You need to read the file in to the memory buffer
file.InputStream.Read(buffer, 0, BellUpload.PostedFile.ContentLength);

4) You need to create a table in mssql
5) I'm using stored procedure to load a file:
ALTER PROCEDURE [dbo].[UploadFile]
@WPS nvarchar(50),
@FileName nvarchar(255),
@FileType nvarchar(50),
@FileContent varbinary(MAX),
@FileSize bigint
insert into Files ([WPS],[FileName], [FileType], [FileContent])
values (@WPS, @FileName, @FileType, @FileContent)

6)Finally you need to upload it by SQL classes
using (SqlConnection _con = new SqlConnection("Data Source=....."))
                using (SqlCommand _cmd = new SqlCommand("UploadFile", _con))
                    _cmd.CommandType = CommandType.StoredProcedure;
                    _cmd.Parameters.AddWithValue("@WPS", WPSfield.Text.Trim());
                    _cmd.Parameters.AddWithValue("@FileName", fi.Name);
                    _cmd.Parameters.AddWithValue("@FileType", file.ContentType);
                    _cmd.Parameters.AddWithValue("@FileContent", buffer);
                    _cmd.Parameters.AddWithValue("@FileSize", BellUpload.PostedFile.ContentLength);

Hope it helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100