Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Group Members,
I use to save the profile image of user in a folder and save user information,But now I need to save image in database.Can any one tell me how to save image in database in c#.net
Posted
Comments
DaveAuld 5-Dec-11 4:58am    
Have you tried searching the articles on CodeProject; Have you tried searching via google. If you had, you would have found yourself an answer quicker than it would have taken to post this question. SQL and Images are one of the most common question on CodeProject (Another being how to send emails!)
thatraja 5-Dec-11 5:15am    
What about
Home work questions?
Gimmecode?
how to send SMS?
how to crack my friend's email ids?
etc.,?

For more please contact Smither-Jones
thatraja 5-Dec-11 5:15am    
Agree with Dave

Images are terated just like any other file: put an upload control on your page, and handle the Upload Click event. Call the following routine:

C#
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }
This probably does more than you need, but I lifted direct from my code.

If you need to know how to display the profile pic, see here: A generic Image-From-DB class for ASP.NET[^]
 
Share this answer
 
To Upload the image to database :
C# CODE:

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = "~\\images\\" + FileUpload1.FileName;
        FileUpload1.SaveAs(Server.MapPath(path));
        
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=arun;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        string sql = string.Format("insert into Products values({0},'{1}','{2}','{3}','{4}')",Txtcode.Text,Txtname.Text,Txtcost.Text,Txtdescr.Text,path);
        cmd.Connection = con;
        cmd.CommandText = sql;

        cmd.ExecuteNonQuery();
        con.Close();


ASPX PAGE CODE:

<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
Share this answer
 
v2
 
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