If Not dtrMyDataReader .IsDBNull(dtrMy DataReader.GetO rdinal("Custome r")) Then
strCustomer = dtrMyDataReader ("Customer").To String()
End If
Where "Customer" is a string data type.
Is there any better code to check for null values in the first line.
Is there any better way the code can be written?
..Net 1.1 and Visual Basic and ODBC connections.
Thank you,
Larry 8 1199
1. faster to use the index and not the column name, but better practice
to use the column name, i'd say:
dim ixCustomer as integer = dr.GetOrdinal(" Customer")
2. better to use typed accessor:
if not dr.IsDBNull(ixC ustomer) then
strCustomer = dr.GetString(ix Customer)
end if
HTH
Well, I'm not particularly familiar with DataReader's but a couple of things
come to mind to make your code "Better":
1. Is the query going to be the same all the time - can you gain an
efficiency perspective by either precomputing the ordinal value of the
field, or precomputing a "pointer/reference"" to the object (assuming the
reader does not recreate it each time)?
2. In the first line (IsDBNull) you go to the trouble of computing the
ordinal of the field (admittedly because IsDBNull only offers this variant),
then you just throw it away and look up the field by name in the second
line. You would gain better efficiency by just holding the ordinal as a
temporary value (eg. within the routine) and using it in both places. Given
that the sample DataReader implementations I have seen simply use a linear
search to compute an Ordinal, that's an awful lot of "lookups" happening
every time, so minimising it would be worthwhile. A dictionary lookup will
be more efficient, but still not as efficient as not doing it.
3. Since I come from a large systems background, the use of "magic
constants" such as hard-coding the "Customer" literal is pretty dodgy in my
opinion. These should be declared as constants somewhere. Having said that,
RAD-style development tools such as VS.NET that encourage you to plug in
"properties " all over the place against objects don't really encourage
constant-reuse so this will have to be something you make the call on. This
is something I'm grappling with at present.
Hope this helps.
Kevin
Thank you to both of you for replying.
Larry
"Kevin Frey" <ke**********@h otmail.com> wrote in message
news:OF******** ******@TK2MSFTN GP09.phx.gbl... Well, I'm not particularly familiar with DataReader's but a couple of
things come to mind to make your code "Better":
1. Is the query going to be the same all the time - can you gain an efficiency perspective by either precomputing the ordinal value of the field, or precomputing a "pointer/reference"" to the object (assuming the reader does not recreate it each time)?
2. In the first line (IsDBNull) you go to the trouble of computing the ordinal of the field (admittedly because IsDBNull only offers this
variant), then you just throw it away and look up the field by name in the second line. You would gain better efficiency by just holding the ordinal as a temporary value (eg. within the routine) and using it in both places.
Given that the sample DataReader implementations I have seen simply use a linear search to compute an Ordinal, that's an awful lot of "lookups" happening every time, so minimising it would be worthwhile. A dictionary lookup will be more efficient, but still not as efficient as not doing it.
3. Since I come from a large systems background, the use of "magic constants" such as hard-coding the "Customer" literal is pretty dodgy in
my opinion. These should be declared as constants somewhere. Having said
that, RAD-style development tools such as VS.NET that encourage you to plug in "properties " all over the place against objects don't really encourage constant-reuse so this will have to be something you make the call on.
This is something I'm grappling with at present.
Hope this helps.
Kevin
"Larry Smith" <LS************ **@hotmail.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. If Not dtrMyDataReader .IsDBNull(dtrMy DataReader.GetO rdinal("Custome r")) Then
strCustomer = dtrMyDataReader ("Customer").To String() End If
Where "Customer" is a string data type.
Is there any better code to check for null values in the first line.
Is there any better way the code can be written?
.Net 1.1 and Visual Basic and ODBC connections.
Thank you,
Larry
Public Function GetString( _
ByVal Reader As DataReader, _
ByVal Column As String _
) As String
Dim idx As Integer = Reader.GetOrdin al(Column)
If Not Reader.IsDBNull (idx)
Return Reader.GetStrin g(idx)
End If
Return String.Empty
End Function
Simple utility function to make all your if...then's a single-line ... for
the most part.
Dim customer As String = GetString(myRea der, "Customer")
HTH :)
Mythran
I am looking for efficient code.
This approach makes the code simpler & makes it maintainable... .
Let me rephrase the question - "Better in terms of efficiency"
Thank you,
Larry
"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. "Larry Smith" <LS************ **@hotmail.com> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. If Not dtrMyDataReader .IsDBNull(dtrMy DataReader.GetO rdinal("Custome r")) Then
strCustomer = dtrMyDataReader ("Customer").To String() End If
Where "Customer" is a string data type.
Is there any better code to check for null values in the first line.
Is there any better way the code can be written?
.Net 1.1 and Visual Basic and ODBC connections.
Thank you,
Larry
Public Function GetString( _ ByVal Reader As DataReader, _ ByVal Column As String _ ) As String Dim idx As Integer = Reader.GetOrdin al(Column) If Not Reader.IsDBNull (idx) Return Reader.GetStrin g(idx) End If Return String.Empty End Function
Simple utility function to make all your if...then's a single-line ... for the most part.
Dim customer As String = GetString(myRea der, "Customer")
HTH :)
Mythran
well to make it super fast, you want the code to do the least work...
a) make sure that value is never NULL in the db, then you don't have to
test for it
b) if you are just returning the customer value (and no other fields)
then make it the output or return parameter of a stored procedure, or
use ExecuteScalar
c) hardcode the ordinal constant.
but as always, code for clarity and maintainability , until performance
becomes an issue.
Thank you,
Larry
<ne**********@g mail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com... well to make it super fast, you want the code to do the least work...
a) make sure that value is never NULL in the db, then you don't have to test for it b) if you are just returning the customer value (and no other fields) then make it the output or return parameter of a stored procedure, or use ExecuteScalar c) hardcode the ordinal constant.
but as always, code for clarity and maintainability , until performance becomes an issue.
First of all, if you're casting the field to a string then why bother
to check if the value is null? Whats wrong with setting strCustomer to
"" which is what you will get when you cast a dbnull to a string?
Avoid having to check for null values by replacing all null values with
an empty string when you retrieve the data from your database. For
example:
select isnull(Customer , "") as Customer from myTable.
Question: Why are you using GetOrdinal? Why not simplify it with:
If Not dtrMyDataReader .IsDBNull(Custo mer") then...
Also, although it doesn't hurt, there really is no reason to use
..ToString() in vb.net.
mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have any preconceived ideas about it. I have
noticed, however, that every programmer I talk to who's aware of Python is
also talking about Ruby. So it seems that Ruby has the potential to compete
with and displace Python. I'm curious on what basis it...
|
by: Michael |
last post by:
Hi,
I'm fairly new at Python, and have the following code that works but isn't
very concise, is there a better way of writing it?? It seems much more
lengthy than python code i have read..... :-)
(takes a C++ block and extracts the namespaces from it)
def ExtractNamespaces(data):
print("Extracting Namespaces")
|
by: Sai Kit Tong |
last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't
make it clear in my original mail. I got a couple of answers but none of
them address my issues directly (See attached response). My first reply
directed me to source code migration but I didn't have the source code. The
second reply mentioned about .NET interoperability (PInvoke I think) but I
MENTIONED THAT I COULDN'T FIND ANY DOCUMENTATION FROM MSDN LIBRARY BASED ON...
|
by: Rob R. Ainscough |
last post by:
I realize I'm learning web development and there is a STEEP learning curve,
but so far I've had to learn:
HTML
XML
JavaScript
ASP.NET using VB.NET
..NET Framework
ADO.NET
SSL
|
by: cwbp17 |
last post by:
Have two oracle tables that have a FK relationship on ID column.
Have one datagrid that displays all of the columns of both tables.
What's the best approach on updating a row from the datagrid back to the
database?
I've used the following code for the DataGird1_UpdateCommand
Option1
| |
by: JoeC |
last post by:
I am a self taught programmer and I have figured out most syntax but
desigining my programs is a challenge. I realize that there are many
ways to design a program but what are some good rules to follow for
creating a program?
I am writing a map game program. I created several objects: board
object that is an array of integers each number 0-5 is a kind of
terrain, a terrain object that is an array of terrain types and each
number of...
|
by: JoeC |
last post by:
I am working on another game project and it is comming along. It is an
improvment over a previous version I wrote. I am trying to write
better programs and often wonder how to get better at programming. I
tend to learn what is useful and gets the job done. I am always
curious if there is some techique I don't know. I read books and study
as well as write programs. My goal is to some day be able to get a job
programming. I have a...
|
by: bh |
last post by:
If I want to loop through the values in a listbox, and get either all items
in the box, or only selected items, based on a boolean variable passed into
a subroutine, which method would be more efficient?
First Method (test allvalues, first and either loop through all items or
selecteditems, accordingly):
Dim dview As DataRowView
Dim AllValues As Boolean = False
If AllValues = True Then
|
by: pamela fluente |
last post by:
I would like to hear your *opinion and advice* on best programming
practice under .NET.
Given that several time we cannot change:
MyCollection.Clear into the instantiation of a NEW MyCollection
because we make orphan some needed reference,
I notice also that several times I can "equivalently" decide whether a
|
by: Pawel_Iks |
last post by:
I've read somewhere that c++ is something more than better c ... then
I talk with my friend and he claimed that c++ is nothing more than
better c ... I tried to explain him that he was wrong but I forgot all
arguments about it. Could someone told something about it?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |