all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

retrive images as files on the disk



Re: retrive images as files on the disk Stefan Delmarco
11/26/2006 10:35:28 PM
sql server programming: Hello Sam,

Some sample C# code for exporting the GIFs in the pubs database. Should all
be self-explanatory ;)

using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace ExportImages
{
public static class Program
{
private static void Main(string[] args)
{
using(SqlConnection conn = new SqlConnection("Server=localhost;Database=pubs;Integrated
Security=SSPI"))
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"
select p.pub_name, pi.logo
from publishers p
inner join pub_info pi
on p.pub_id = pi.pub_id";

conn.Open();
using(IDataReader reader = cmd.ExecuteReader())
{
byte[] buffer = new byte[10 * 1024];
while(reader.Read())
{
string filename = string.Format("{0}.gif", reader.GetString(0));
long bytesRead = 0;
long offset = 0;
using(FileStream stream = File.Open(filename, FileMode.OpenOrCreate))
{
while ((bytesRead = reader.GetBytes(1, offset, buffer,
0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, (int)bytesRead);
offset += bytesRead;
}
}
}
}
}
}
}
}

Cheers,
Stefan Delmarco

http://www.fotia.co.uk

[quoted text, click to view]

retrive images as files on the disk sam
11/26/2006 11:16:32 PM
how can i retrive programicly images stored in an table and put them
as files on the disk?

tanks

Re: retrive images as files on the disk sam
12/21/2006 12:00:00 AM
i'm not familiar with C#, is there a method with SQL PROCEDURE?


[quoted text, click to view]

AddThis Social Bookmark Button