Searching a table

R

Roy Gourgi

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();


}

}

}
 
P

pvdg42

Roy Gourgi said:
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.
 
R

Roy Gourgi

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();

}

}

}
 
P

pvdg42

Roy Gourgi said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top