Read Data

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following code:

1 ' Code that creates the connection, parameters, etc
2 ' ...
3
4 ' Execute the command
5
6 ' ???? THE CODE I NEED TO KNOW
7
8 value.Close()
9 connection.Close()

Consider that I always get records with only one field named
"ContentHtml".

If there is only one record I want to get the value in ContentHtml
field.
If there is no records I want to define result = "NoRecords" and do
nothing else.
If there are more than one record I want to define result =
"MultipleRecords" and do nothing else.

My question is:
1. Should I do this in my MS SQL code?
2. How could I do this in my .NET code?
I could use a DataReader but I am not sure if this is the right
way:

Dim value As DbDataReader = command.ExecuteReader()
.... ???

As I said I need to determine if the number of records is 0, 1 or
more than 1.

Could somebody help me out?

Thanks,
Miguel
 
That's fairly simple.

First, test of the datareader is null or has no rows.

If it doesn't, then display the no rows.

Now, the rest I'm going to write in C# because my VB knowledge hasn't been
used in way too long.

// create a variable to hold a running count.
int count = 0;

// create a variable to hold the output string.
string outputText = string.Empty;

if((value != null) && (value.HasRows))
{
while(value.Read())
{
if(count > 1)
{
outputText = "Multiple Records";
break;
}
else
{
// next get the output value. If there is more than one record
this will be overriden
outputText = value[value.GetOrdinal(ContentHtml)].ToString();
count++;
}
}
}
else
{
outputText = NoRecords;
}

You may find it easier however to do this in the database itself if you're
using stored procedures since there you can easily test for the number of
rows returned and simply return a parameter or field with the output string
already defined.
 
I was doing that in the SQL database stored procedure but I was advised
to do this in my VB.NET code ...

Each persons advices me a different thing. I am completely lost ... :-)

Thanks,
Miguel

Mark said:
That's fairly simple.

First, test of the datareader is null or has no rows.

If it doesn't, then display the no rows.

Now, the rest I'm going to write in C# because my VB knowledge hasn't been
used in way too long.

// create a variable to hold a running count.
int count = 0;

// create a variable to hold the output string.
string outputText = string.Empty;

if((value != null) && (value.HasRows))
{
while(value.Read())
{
if(count > 1)
{
outputText = "Multiple Records";
break;
}
else
{
// next get the output value. If there is more than one record
this will be overriden
outputText = value[value.GetOrdinal(ContentHtml)].ToString();
count++;
}
}
}
else
{
outputText = NoRecords;
}

You may find it easier however to do this in the database itself if you're
using stored procedures since there you can easily test for the number of
rows returned and simply return a parameter or field with the output string
already defined.


--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006





shapper said:
Hello,

I have the following code:

1 ' Code that creates the connection, parameters, etc
2 ' ...
3
4 ' Execute the command
5
6 ' ???? THE CODE I NEED TO KNOW
7
8 value.Close()
9 connection.Close()

Consider that I always get records with only one field named
"ContentHtml".

If there is only one record I want to get the value in ContentHtml
field.
If there is no records I want to define result = "NoRecords" and do
nothing else.
If there are more than one record I want to define result =
"MultipleRecords" and do nothing else.

My question is:
1. Should I do this in my MS SQL code?
2. How could I do this in my .NET code?
I could use a DataReader but I am not sure if this is the right
way:

Dim value As DbDataReader = command.ExecuteReader()
.... ???

As I said I need to determine if the number of records is 0, 1 or
more than 1.

Could somebody help me out?

Thanks,
Miguel
 
Back
Top