Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sybase Binary Columns [modified]

Sybase Binary Columns [modified]

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mobius111001
    wrote on last edited by
    #1

    Can anyone tell me what would be the appropriate data type to use in C# when reading and writing to binary fields in a Sybase database? Thank you in advance. Modified: I have a byte[] populated with 33 values. I'm using a stored proc that has a binary parameter. I'm assigning the parameter the byte[], but I'm only getting 1-byte to write and I'm not even sure it has anything to do with the value that should be written.

    modified on Thursday, January 15, 2009 4:08 PM

    D 1 Reply Last reply
    0
    • M mobius111001

      Can anyone tell me what would be the appropriate data type to use in C# when reading and writing to binary fields in a Sybase database? Thank you in advance. Modified: I have a byte[] populated with 33 values. I'm using a stored proc that has a binary parameter. I'm assigning the parameter the byte[], but I'm only getting 1-byte to write and I'm not even sure it has anything to do with the value that should be written.

      modified on Thursday, January 15, 2009 4:08 PM

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You haven't given much information to go on, so the generic answer would be byte[].

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      modified on Thursday, January 15, 2009 1:09 PM

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You haven't given much information to go on, so the generic answer would be byte[].

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        modified on Thursday, January 15, 2009 1:09 PM

        M Offline
        M Offline
        mobius111001
        wrote on last edited by
        #3

        Please see original post. I modified it. Thanks again.

        D 1 Reply Last reply
        0
        • M mobius111001

          Please see original post. I modified it. Thanks again.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Without seeing the code you're using to pass the byte array to the database, it's difficult to say what's going wrong.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          M 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Without seeing the code you're using to pass the byte array to the database, it's difficult to say what's going wrong.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            M Offline
            M Offline
            mobius111001
            wrote on last edited by
            #5

            //////////////////////////////////////// // SQL //////////////////////////////////////// CREATE PROCEDURE InsertBinData( @binData BINARY ) BEGIN INSERT INTO Table1 ( BinData ) VALUES @binData END //////////////////////////////////////// // C# //////////////////////////////////////// byte[] data = new byte[33]; // function call to fill byte array here command.CommandType = CommandType.StoredProcedure; command.Parameters.Clear(); command.Parameters.AddWithValue( "@binData", data ); command.CommandText = "InsertBinData"; Command.ExecuteNonQuery();

            D 1 Reply Last reply
            0
            • M mobius111001

              //////////////////////////////////////// // SQL //////////////////////////////////////// CREATE PROCEDURE InsertBinData( @binData BINARY ) BEGIN INSERT INTO Table1 ( BinData ) VALUES @binData END //////////////////////////////////////// // C# //////////////////////////////////////// byte[] data = new byte[33]; // function call to fill byte array here command.CommandType = CommandType.StoredProcedure; command.Parameters.Clear(); command.Parameters.AddWithValue( "@binData", data ); command.CommandText = "InsertBinData"; Command.ExecuteNonQuery();

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              mobius111001 wrote:

              byte[] data = new byte[33]; // function call to fill byte array here command.CommandType = CommandType.StoredProcedure; command.Parameters.Clear(); command.Parameters.AddWithValue( "@binData", data ); command.CommandText = "InsertBinData"; Command.ExecuteNonQuery();

              Don't use AddWithValue. Use the full technique of creating a Parameter object, setting it's database type and direction properly and then assign the value to it and add it to the collection. Here, you're assuming that the object will correctly guess the correct database type to use, when, in fact, it can get it wrong.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              M 1 Reply Last reply
              0
              • D Dave Kreskowiak

                mobius111001 wrote:

                byte[] data = new byte[33]; // function call to fill byte array here command.CommandType = CommandType.StoredProcedure; command.Parameters.Clear(); command.Parameters.AddWithValue( "@binData", data ); command.CommandText = "InsertBinData"; Command.ExecuteNonQuery();

                Don't use AddWithValue. Use the full technique of creating a Parameter object, setting it's database type and direction properly and then assign the value to it and add it to the collection. Here, you're assuming that the object will correctly guess the correct database type to use, when, in fact, it can get it wrong.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                M Offline
                M Offline
                mobius111001
                wrote on last edited by
                #7

                i still only get the byte[0] to write out. OdbcConnection cn = new OdbcConnection(); // assuming db connection made correction OdbcCommand cm = new OdbcCommand(); Random r = new Random( ( int )DateTime.Now.Ticks ); OdbcParameter op; cm.Connection = cn; byte[] b = new byte[32]; for ( int i = 0; i < b.Length; i++ ) { b[i] = ( byte )( r.Next() % 256 ); } cm.CommandType = CommandType.StoredProcedure; cm.Parameters.Clear(); op = cm.Parameters.Add( "@binVal", OdbcType.Binary ); op.Direction = ParameterDirection.Input; op.Size = b.Length; op.Value = b; cm.CommandText = "sp_SaveBinaryVal"; cn.Open(); try { cm.ExecuteNonQuery(); } finally { if ( cn.State == ConnectionState.Open ) { cn.Close(); } }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups