473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HELP: GetSqlBinary conversion in C# and VB.NET

Anyone know what the VB.NET equivalent of this C# statement is...

byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

Tried as hard as I could to do it in VB.NET but I cannot seem to do it.
Nov 20 '05 #1
6 2755
"Gareth Parris" <gc******@hotmail.comm> schrieb
Anyone know what the VB.NET equivalent of this C# statement is...

byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

Tried as hard as I could to do it in VB.NET but I cannot seem to do
it.


Untested:
dim segbody as byte() = directcast(sqlReader.GetSqlBinary(3), byte())
--
Armin

Nov 20 '05 #2
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus
Nov 20 '05 #3
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB
example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of bytes, so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte()) 'Fails Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus

Nov 20 '05 #4
Fergus. Excellant stuff that worked nicely!

"Gareth Parris" <gc******@hotmail.comm> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
Thanks Fergus. Funnily enough the C# code works perfectly. I'll try your VB example right now.

Cheers,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Hi Gareth,

|| byte [] segBody = (byte []) sqlReader.GetSqlBinary(3);

GetSqlBinary returns a SqlBinary structure rather than an array of

bytes,
so a straight conversion to VB fails, as you have found. Does that C#
statement actually work?

These fail:
Dim segBody1 As Byte() _
= DirectCast (sqlReader.GetSqlBinary(3), Byte())

'Fails
Dim segBody2 As Byte() _
= CType (sqlReader.GetSqlBinary(3), Byte()) 'Fails

See if this code [untested] gives you some joy:
(Imports System.Data.SqlTypes)
Dim sqlBinValue As SqlBinary = sqlReader.GetSqlBinary(3)
Dim segBody As Byte() = SqlBinary.op_Explicit (sqlBinValue)

Regards,
Fergus


Nov 20 '05 #5
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.

C# provides the ability to define explicit conversions which take place
when a variable is cast from one form to another. op_Explicit is the function
that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus
Nov 20 '05 #6
I was not aware of that. That makes more sense now.

Many thanks again,
Gareth

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
Hi Gareth,

That op_ Explicit was niggling away in my mind and I've just remembered.
C# provides the ability to define explicit conversions which take place when a variable is cast from one form to another. op_Explicit is the function that allows other languages to access that convesion.

So
(byte []) sqlReader.GetSqlBinary(3);
and
SqlBinary.op_Explicit (sqlReader.GetSqlBinary(3));
are equivalent.

Regards,
Fergus

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thames | last post by:
Hi, I have to repost my question for help. Yesterday I set up two DB UDB ESE V8.2 server on linux and windows platform, respectively. Both servers have been connected without any problems...
27
by: Chuck Grimsby | last post by:
(Repost, due to lack of submissions...) The Microsoft Access Product Group (the people who build Microsoft Access) want your help! One of the main things we're working on for the near future...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
5
by: Gareth Parris | last post by:
Anyone know what the VB.NET equivalent of this C# statement is... byte segBody = (byte ) sqlReader.GetSqlBinary(3); Tried as hard as I could to do it in VB.NET but I cannot seem to do it.
3
by: ASP .NET Newbie | last post by:
I am trying to insert a new record into my database, and have it return a uniqueidentifier as the "newcatid". I don't use integers as my "id"'s, but rather uniqueidentifiers. Here's my Stored...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Gregory Edigaroff | last post by:
Hello, Everybody! Ok, I urgently need the solution for a following task in C++. This is the task from programmers contest, so I believe somebody have a solution for it. I need either a full...
0
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
2
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
1
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.