How to pass binary data to a stored procedure from a c# program

I need to pass data to an input parameter of a stored procedure which is defined as Binary(8)

This is what I have so far , but it does not seem to do any processing. So if someone couls let me know if

public static void DBProcedure(string processName, byte[] beginTimeStamp, byte[] endTimeStamp, string destination, params string[] tradingArea)
            Logger.LogMessage(Constants.Information, "Begin DB Processing : " + Thread.CurrentThread.Name + " : " + DateTime.Now.ToLocalTime());
            SqlConnection connection = new SqlConnection(connectionString);
                SqlCommand command = new SqlCommand(GetProcName(destination), connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ProcessName", SqlDbType.VarChar, 200).Value = processName;
                command.Parameters.Add("@BeginTimeStamp", SqlDbType.Binary).Value = beginTimeStamp;
                command.Parameters.Add("@EndTimeStamp", SqlDbType.Binary).Value = endTimeStamp;
                               if (destination.ToUpper() != Constants.MAIN && destination.ToUpper() != Constants.SEPARATE_DB)
                    command.Parameters.Add("@DestinationTables", SqlDbType.VarChar).Value = destination;
                if ((tradingArea.Length > 0) && (destination != Constants.MAIN))
                    command.Parameters.Add("@TradingArea", SqlDbType.VarChar).Value = tradingArea[0];
                command.CommandTimeout = timeout;
            catch (Exception ex)
                Console.WriteLine("Error : " + ex.Message);              
                Logger.LogMessage(Constants.Error, ex.Message);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

countrymeisterAuthor Commented:
and here is the query string that I pass to the method below for each call one to get minimum timestamp and the other for maxtimestamp

SELECT MIN(TimeStamp) AS MinTimeStamp FROM Table1 (nolock) WHERE LastModifyTime BETWEEN  '1/23/2008' AND '1/23/2008 23:59:00.000'

SELECT MAX(TimeStamp) AS MaxTimeStamp FROM Table1 (nolock) WHERE LastModifyTime BETWEEN  '1/23/2008' AND '1/23/2008 23:59:00.000'

readerFiedl is MinTimeStamp for the first call and maxtimestamp for the second call

I get the data in a byte array of length 36, whcih I then pass to the DBProcedure method above


public static byte[] DBReader(string queryString, string readerField)
            Byte[] minTimeStamp = null;
            using (SqlConnection connection = new SqlConnection(
                SqlCommand command = new SqlCommand(queryString, connection);
                command.CommandTimeout = timeout;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                    MemoryStream ms = new MemoryStream();
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(ms, reader[readerField]);
                    minTimeStamp = ms.ToArray();
                return minTimeStamp;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.