Insert/Update image

Discussion in 'ASP .Net' started by DavidC, Jan 26, 2010.

  1. DavidC

    DavidC Guest

    I have an asp.net web page that I want to be able to read an image file from
    the filesystem on the web server and insert it into a SQL Server 2008
    database image data type. The insert command would be similar to below. I
    have never done this before so I need some direction on how to accomplish
    this in asp.net using 3.5 framework. Also, is image the correct data type?
    Thanks.

    INSERT INTO dbo.ClientSignatures (ClientSignature) VALUES (some image)
    --
    David
     
    DavidC, Jan 26, 2010
    #1
    1. Advertising

  2. On Jan 26, 6:19 pm, DavidC <> wrote:
    > I have an asp.net web page that I want to be able to read an image file from
    > the filesystem on the web server and insert it into a SQL Server 2008
    > database image data type.  The insert command would be similar to below..  I
    > have never done this before so I need some direction on how to accomplish
    > this in asp.net using 3.5 framework.  Also, is image the correct data type?
    > Thanks.
    >
    > INSERT INTO dbo.ClientSignatures (ClientSignature) VALUES (some image)
    > --
    > David


    Hi David,

    reading and writing files to a database is quite easy. You just need
    to read the file as a byte[] array and send to the database. The code
    could be similar to this:

    FileStream fs = new FileStream("fileName.jpg", FileMode.Open,
    FileAccess.Read);
    byte[] data = new byte[fs.Length];
    fs.Read(data, 0, Convert.ToInt32(fs.Length));
    fs.Close();

    sql = "INSERT INTO dbo.ClientSignatures (ClientSignature) VALUES
    (@fileData)";
    SqlCommand cmd = new SqlCommand(sql, myConnection);
    cmd.Parameters.Add(new SqlParameter("@fileData", data));

    myConnection.Open();
    cmd.ExecuteNonQuery();

    As you see there are just few lines

    Hope this helps
     
    Alexey Smirnov, Jan 26, 2010
    #2
    1. Advertising

  3. On Jan 26, 6:19 pm, DavidC <> wrote:
    > I have an asp.net web page that I want to be able to read an image file from
    > the filesystem on the web server and insert it into a SQL Server 2008
    > database image data type.  The insert command would be similar to below..  I
    > have never done this before so I need some direction on how to accomplish
    > this in asp.net using 3.5 framework.  Also, is image the correct data type?
    > Thanks.
    >
    > INSERT INTO dbo.ClientSignatures (ClientSignature) VALUES (some image)
    > --
    > David


    Regarding image data type. Although the image data type can be used
    for your task, Microsoft recommended to avoid using it.

    Quote: "ntext, text, and image data types will be removed in a future
    version of Microsoft SQL Server. Avoid using these data types in new
    development work, and plan to modify applications that currently use
    them. Use nvarchar(max), varchar(max), and varbinary(max) instead."

    http://msdn.microsoft.com/en-us/library/ms187993.aspx

    So, if you plan to use in the future the next version of SQL Server,
    it's better to use binary, or varbinary.

    There is an article about using these types, have a look
    http://www.databasejournal.com/features/mssql/article.php/3719221
    http://www.databasejournal.com/features/mssql/article.php/3724556
    http://www.databasejournal.com/features/mssql/article.php/3732256
    http://www.databasejournal.com/features/mssql/article.php/3738276

    Hope this helps
     
    Alexey Smirnov, Jan 26, 2010
    #3
  4. DavidC

    DavidC Guest

    I agree. I have changed it to varbinary(max).
    Thank you for your help. I was simple.
    --
    David


    "Alexey Smirnov" wrote:

    > On Jan 26, 6:19 pm, DavidC <> wrote:
    > > I have an asp.net web page that I want to be able to read an image file from
    > > the filesystem on the web server and insert it into a SQL Server 2008
    > > database image data type. The insert command would be similar to below.. I
    > > have never done this before so I need some direction on how to accomplish
    > > this in asp.net using 3.5 framework. Also, is image the correct data type?
    > > Thanks.
    > >
    > > INSERT INTO dbo.ClientSignatures (ClientSignature) VALUES (some image)
    > > --
    > > David

    >
    > Regarding image data type. Although the image data type can be used
    > for your task, Microsoft recommended to avoid using it.
    >
    > Quote: "ntext, text, and image data types will be removed in a future
    > version of Microsoft SQL Server. Avoid using these data types in new
    > development work, and plan to modify applications that currently use
    > them. Use nvarchar(max), varchar(max), and varbinary(max) instead."
    >
    > http://msdn.microsoft.com/en-us/library/ms187993.aspx
    >
    > So, if you plan to use in the future the next version of SQL Server,
    > it's better to use binary, or varbinary.
    >
    > There is an article about using these types, have a look
    > http://www.databasejournal.com/features/mssql/article.php/3719221
    > http://www.databasejournal.com/features/mssql/article.php/3724556
    > http://www.databasejournal.com/features/mssql/article.php/3732256
    > http://www.databasejournal.com/features/mssql/article.php/3738276
    >
    > Hope this helps
    > .
    >
     
    DavidC, Jan 26, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JpMaxMan
    Replies:
    3
    Views:
    8,888
    JpMaxMan
    Mar 15, 2006
  2. cannontrodder
    Replies:
    1
    Views:
    764
    cannontrodder
    Jul 25, 2006
  3. Replies:
    1
    Views:
    481
  4. Replies:
    1
    Views:
    1,873
    Albert Hopkins
    Dec 6, 2008
  5. JpMaxMan

    ASP.NET 2.0 ImageField Upload Image Insert / Update

    JpMaxMan, Mar 8, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    159
    JpMaxMan
    Mar 8, 2006
Loading...

Share This Page