Sybase Binary Columns [modified]
-
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
-
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
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, 2008modified on Thursday, January 15, 2009 1:09 PM
-
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, 2008modified on Thursday, January 15, 2009 1:09 PM
Please see original post. I modified it. Thanks again.
-
Please see original post. I modified it. Thanks again.
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 -
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//////////////////////////////////////// // 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();
-
//////////////////////////////////////// // 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();
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 -
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, 2008i 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(); } }