Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert photo in sql server 2005 with C# code?
Posted

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.
 
Share this answer
 
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):
C#
FileInfo fi = new FileInfo(BellUpload.FileName);

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

3) You need to read the file in to the memory buffer
C#
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:
SQL
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
C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900