System.Byte[] in vs2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Unlhbg==?=

    System.Byte[] in vs2005

    How do I read a blob field of a database using System.Byte[]?
    A sample code would be appreciated.
  • Paul Musso

    #2
    Re: System.Byte[] in vs2005

    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=Northwi nd;UID=ctester; PWD=password";
    try {
    SqlConnection conn = new SqlConnection(s Conn);
    SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM Categories WHERE
    CategoryName='B uilder'", conn);
    cn.Open();
    SqlDataReader sdr = cmd.ExecuteRead er();
    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.Writ e);

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


    Sources :


    --
    Paul Musso


    Comment

    • DrewCE

      #3
      Re: System.Byte[] in vs2005

      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=pas sword";
      try
      {
      Byte[] blob = null;
      using (SqlConnection conn = new SqlConnection(s Conn))
      {
      conn.Open();
      SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM
      Categories WHERE CategoryName='B uilder'", conn);
      using (SqlDataReader sdr = cmd.ExecuteRead er())
      {
      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.Writ e))
      {
      fs.Write(blob, 0, blob.Length);
      }
      }
      catch (SqlException e)
      {
      Console.WriteLi ne("SQL Exception: " + e.Message);
      }
      catch (Exception e)
      {
      Console.WriteLi ne("Exception: " + e.Message);
      }

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

      -Drew


      "Paul Musso" <paulm@exakis.c omwrote in message
      news:%23ugtcmQH JHA.2156@TK2MSF TNGP05.phx.gbl. ..
      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=Northwi nd;UID=ctester; PWD=password";
      try {
      SqlConnection conn = new SqlConnection(s Conn);
      SqlCommand cmd = new SqlCommand("SEL ECT Picture FROM Categories WHERE
      CategoryName='B uilder'", conn);
      cn.Open();
      SqlDataReader sdr = cmd.ExecuteRead er();
      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.Writ e);
      >
      fs.Write(blob, 0, blob.Length);
      fs.Close();
      } catch (SqlException e){
      Console.WriteLi ne("SQL Exception: " + e.Message);
      } catch (Exception e) {
      Console.WriteLi ne("Exception: "+ e.Message);
      }
      >
      >
      Sources :

      >
      --
      Paul Musso
      >
      >

      Comment

      • Marc Gravell

        #4
        Re: System.Byte[] in vs2005

        Further to the other replies; if the BLOB is large, you should use
        sequential access:



        Marc

        Comment

        • =?Utf-8?B?Unlhbg==?=

          #5
          Re: System.Byte[] in vs2005

          Thanks to you all; i will try these examples and update you soon.

          "Marc Gravell" wrote:
          Further to the other replies; if the BLOB is large, you should use
          sequential access:
          >

          >
          Marc
          >

          Comment

          Working...