Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am trying to insert photo in sql server 2005 with C# code?
Posted 16-Jan-13 4:07am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello!
 
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
AS
BEGIN
insert into Files ([WPS],[FileName], [FileType], [FileContent])
values (@WPS, @FileName, @FileType, @FileContent)
END
 
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);
 
                    _con.Open();
                    _cmd.ExecuteNonQuery();
                    _con.Close();
 
                }
 
Hope it helps.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid