System.Byte[] in vs2005

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

How do I read a blob field of a database using System.Byte[]?
A sample code would be appreciated.
 
Ryan avait énoncé :
How do I read a blob field of a database using System.Byte[]?
A sample code would be appreciated.

Hi Ryan,

This is an example

Byte[] blob = null;
FileStream fs = null;
const string sConn = "server=(local);Initial
Catalog=Northwind;UID=ctester;PWD=password";
try {
SqlConnection conn = new SqlConnection(sConn);
SqlCommand cmd = new SqlCommand("SELECT Picture FROM Categories WHERE
CategoryName='Builder'", conn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();

blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))];
sdr.GetBytes[0, 0, blob, 0, blob.Length);
sdr.Close();
conn.Close();
fs = new FileStream("c:\\Builder.doc", FileMode.Create,
FileAccess.Write);

fs.Write(blob, 0, blob.Length);
fs.Close();
} catch (SqlException e){
Console.WriteLine("SQL Exception: " + e.Message);
} catch (Exception e) {
Console.WriteLine("Exception: "+ e.Message);
}


Sources :
http://articles.techrepublic.com.com/5100-10878_11-5766889.html
 
Building on Paul's example; I'd prefer to make use of the "using" statement
to make sure we don't leave anything open...

const string sConn =
"server=(local);InitialCatalog=Northwind;UID=ctester;PWD=password";
try
{
Byte[] blob = null;
using (SqlConnection conn = new SqlConnection(sConn))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT Picture FROM
Categories WHERE CategoryName='Builder'", conn);
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();

blob = new Byte[(sdr.GetBytes(0, 0, null, 0,
int.MaxValue))];
sdr.GetBytes(0, 0, blob, 0, blob.Length);
}
}

using (FileStream fs = new FileStream("c:\\Builder.doc",
FileMode.Create, FileAccess.Write))
{
fs.Write(blob, 0, blob.Length);
}
}
catch (SqlException e)
{
Console.WriteLine("SQL Exception: " + e.Message);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}

Keep in mind, I didn't verfiy the code is correct. Just added use of
"using".

-Drew


Paul Musso said:
Ryan avait énoncé :
How do I read a blob field of a database using System.Byte[]?
A sample code would be appreciated.

Hi Ryan,

This is an example

Byte[] blob = null;
FileStream fs = null;
const string sConn = "server=(local);Initial
Catalog=Northwind;UID=ctester;PWD=password";
try {
SqlConnection conn = new SqlConnection(sConn);
SqlCommand cmd = new SqlCommand("SELECT Picture FROM Categories WHERE
CategoryName='Builder'", conn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();

blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))];
sdr.GetBytes[0, 0, blob, 0, blob.Length);
sdr.Close();
conn.Close();
fs = new FileStream("c:\\Builder.doc", FileMode.Create, FileAccess.Write);

fs.Write(blob, 0, blob.Length);
fs.Close();
} catch (SqlException e){
Console.WriteLine("SQL Exception: " + e.Message);
} catch (Exception e) {
Console.WriteLine("Exception: "+ e.Message);
}


Sources :
http://articles.techrepublic.com.com/5100-10878_11-5766889.html
 

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

Back
Top