Insert/Update image

D

DavidC

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)
 
G

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)

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
 
G

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)

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
 
D

DavidC

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top