From: DavidC on
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
From: Alexey Smirnov on
On Jan 26, 6:19 pm, DavidC <dlch...(a)lifetimeinc.com> 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
From: Alexey Smirnov on
On Jan 26, 6:19 pm, DavidC <dlch...(a)lifetimeinc.com> 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
From: DavidC on
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 <dlch...(a)lifetimeinc.com> 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
> .
>