Insert/Update image

  • Thread starter Thread starter DavidC
  • Start date Start date
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)
 
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
 
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
 
Back
Top