Reading Blobs from SQL and using it in an Image control of a webfo

G

Guest

Please This is queit a task av been trying to solve. Could anyone please tell
me how to read a blob from an SQL using a dataset and stuffing this image
into the Image control of a webform.
Any suggetion that would help would be highly appreciated.
 
E

Eliyahu Goldin

This is an example of the Page_Load event of a page GetImage.aspx:

protected void Page_Load(object sender, System.EventArgs e)
{
// get image id
string imageId = this.Request.Params["id"];

System.Data.SqlTypes.SqlBinary image; // image from database
string sql = String.Format ("SELECT photo FROM customers WHERE
customer_id = {0}", imageId);

// get image from database
SqlConnection conn = new SqlConnection (myConnectionString);
SqlCommand com = new qlCommand (sql, conn);
conn.Open();
try
{
SqlDataReader reader = dba.RunSelect (sql);
reader.Read ();
image = reader.GetSqlBinary(0);
reader.Close ();
}
finally
{
conn.Close ();
}

if (!image.IsNull && image.Value.Length != 0)
{
// stream image down to client
this.Response.ContentType = "image/gif";
this.Response.BinaryWrite (image.Value);
}
}

You would use this page as an url for your image control in format
"GetImage.aspx?id=xx".

Eliyahu
 
E

Erik Funkenbusch

Any suggetion that would help would be highly appreciated.

I suggest NOT using the database to store images, unless they're fairly
small ones. Instead, store the image on the disk, and store a filename
pointer to it in the database. This will make your database smaller, less
prone to fragmentation, and reduce the bandwidth being consumed by record
requests.

In my opinion, storing images in databases takes far more resources than
storing them in the file system.
 
E

Erik Funkenbusch

Often, security considerations make using database mandatory.

That doesn't make any sense. Any security consideration you can put in
place via SQL can be put in place using file permissions.
 
E

Eliyahu Goldin

In SQL Server you can setup security rights for sql logons and roles. File
permissions can be granted to windows accounts, not to sql ones.

Eliyahu
 
E

Erik Funkenbusch

In SQL Server you can setup security rights for sql logons and roles. File
permissions can be granted to windows accounts, not to sql ones.

Windows also has security rights and roles. Again, anything you can define
in SQL can be defined in Windows as well. If you're stuck on using SQL for
your security, it's pathetically easy to validate security through SQL to
access files on the hard disk.

Security is not a valid reason in my opinion.
 
R

Rune B

Eliyahu Goldin said:
Often, security considerations make using database mandatory.

Well, even more often the database could be accessed across domains, and
through different applications (or application layers) that does not allow
the use of a std. filesystem access.
- in that case the Database seems the more flexible choice.

R-)
 
G

Guest

I want to thank you all for ur replies. Every contribution made good sence,
even broadens my idea of the issue.

Thank you very much one and all.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top