471,337 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Searching a table

Hi,

I want to search my table for a particular value and know if it found it. I
tried the code below, but the ExecuteNonQuery only returns a value of -1 so
it does not tell me whether it found it or not. How can I do a search then
and know if it found the value.

TIA
Roy

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

int XXX = 0;
string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\rbc\ dbEnum2BLK.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

string strCommand = "SELECT COUNT(*) FROM tblEnum2BLK WHERE MsPt1_2=2";

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand, oConnection);

oCommand.Connection.Open();

XXX = oCommand.ExecuteNonQuery();

Console.WriteLine("xxxx {0}", XXX);

Console.ReadLine();

oCommand.Connection.Close();
}

}

}
Dec 4 '05 #1
3 1431

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:Zu*******************@wagner.videotron.net...
Hi,

I want to search my table for a particular value and know if it found it.
I tried the code below, but the ExecuteNonQuery only returns a value of -1
so it does not tell me whether it found it or not. How can I do a search
then and know if it found the value.

TIA
Roy

You might want to try the ExecuteReader method using a query that specifies
the value you are looking for in the WHERE clause, then check the number of
rows returned. If the number of rows = 0, the value was not found.
Dec 4 '05 #2
I also tried this, but the value that is returned by the
datareader.recordsaffected is -1. How come?

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\rbc\ dbEnum2BLK.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

string strCommand = "SELECT * FROM tblEnum2BLK WHERE MsPt1_2=2";

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand, oConnection);

oCommand.Connection.Open();

SqlDataReader myReader;

myReader = oCommand.ExecuteReader();

try

{

while (myReader.Read())

{

Console.WriteLine(myReader.RecordsAffected);

}

}

finally

{

myReader.Close();

oConnection.Close();

}

Console.ReadLine();

}

}

}
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:Zu*******************@wagner.videotron.net...
Hi,

I want to search my table for a particular value and know if it found it.
I tried the code below, but the ExecuteNonQuery only returns a value of -1
so it does not tell me whether it found it or not. How can I do a search
then and know if it found the value.

TIA
Roy

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

int XXX = 0;
string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\rbc\ dbEnum2BLK.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

string strCommand = "SELECT COUNT(*) FROM tblEnum2BLK WHERE MsPt1_2=2";

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand, oConnection);

oCommand.Connection.Open();

XXX = oCommand.ExecuteNonQuery();

Console.WriteLine("xxxx {0}", XXX);

Console.ReadLine();

oCommand.Connection.Close();
}

}

}

Dec 4 '05 #3

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:2k*******************@wagner.videotron.net...
I also tried this, but the value that is returned by the
datareader.recordsaffected is -1. How come?

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\rbc\ dbEnum2BLK.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

string strCommand = "SELECT * FROM tblEnum2BLK WHERE MsPt1_2=2";

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand, oConnection);

oCommand.Connection.Open();

SqlDataReader myReader;

myReader = oCommand.ExecuteReader();

try

{

while (myReader.Read())

{

Console.WriteLine(myReader.RecordsAffected);

}

}

finally

{

myReader.Close();

oConnection.Close();

}

Console.ReadLine();

}

}

}

Look up the article "Retrieving Data Using The DataReader" in the MSDN help
and implement it that way. Once you implement the reader and execute the
SQL, you can check the SqlDataReader HasRows property to see if any rows
were returned.
Dec 5 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michi | last post: by
33 posts views Thread by Geoff Jones | last post: by
3 posts views Thread by Aaron | last post: by
3 posts views Thread by db2admin | last post: by
reply views Thread by rosydwin | last post: by

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.