Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
knudmt
Starting Member
12 Posts |
Posted - 2010-07-21 : 18:05:05
|
| Hello,I am a newbie. Here is the problem description:have an asp.net web page that i am creating. Have a column in table named Image datatype is image. using visual studio 2010. How can I insert an image into database using sql query? INSERT INTO Vehicle (Image)VALUES (??????) <-- file location?? |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2010-07-23 : 20:11:47
|
| You can do it, you have to convert the image into a byte stream in your web page and then add to db via your app.this kind of works below. it's C#byte[] Image;SqlCommand SqlUpdate = new SqlCommand();FileStream fs = new FileStream(@"bob.jpg", FileMode.Open, FileAccess.Read);Image = new byte[fs.Length];fs.Read(Image, 0, System.Convert.ToInt32(fs.Length));fs.Close();SqlUpdate = new SqlCommand("update table set image = @pic where id = 1", objConnection);SqlUpdate.Parameters.AddWithValue("@pic", Image);SqlUpdate.ExecuteNonQuery(); |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-24 : 02:31:55
|
| Are you sure you should be storing the image in the database? |
 |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2010-07-24 : 04:57:12
|
| Kristen, It's an acceptable practice to stores images under 2 gb in a db, but it's probably better to use the file stream data type if he's using sql 2008. This gives the flexibility of having the images on the file system but with the access capabilities of a db.I personally put images in a db because it's the only way I've found to embed images in reports when using crystal reports. Hence the C#.Pete |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-25 : 03:42:06
|
"It's an acceptable practice to stores images under 2 gb in a db"Is it? I would have thought it more important to consider what the images are being used for and the requirements of the application.For images that must be ATOMically linked with data records, transferred to other machines with a database without "losing some", or images that need to be handled in a very secure way, then its very probably a good idea. (and I now learn it may be the only way tog et images into Crystal reports )To store some images for a web site it would very probably be a terrible idea. Hence why I asked the question. |
 |
|
|
|
|
|
|
|