473,387 Members | 1,391 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.

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 1481

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michi | last post by:
I was wondering what the best solution is for making large numbers of TEXT (or BLOB?) fields searchable. For example, if I have a forum, what is the best way to be able to search for specific...
8
by: Stuart Clark | last post by:
Hello, I haven't done much with ASP but now I'm giving it a go! However, I'd like to ask if anybody knows if what I need to do is possible. I have a database with a lot of tables. Each table...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
5
by: justobservant | last post by:
When more than one keyword is typed into a search-query, most of the search-results displayed indicate specified keywords scattered throughout an entire website of content i.e., this is shown as...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
3
by: db2admin | last post by:
Hello all, I have table with CLOB column length of ~65K. application is searching from this table by CLOB and i can not create index on it. I know IBM recommends using DB2 Net Search Extender...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
20
by: tkip | last post by:
Hello everyone. I have been working on this DB for quite some time and still can't figure out a way to do what I want to do. It's a database that keep track of drawings and engineering change etc....
3
repath
by: repath | last post by:
Hi, I use DCount function to count the number of entries for the existing customer by name of the customer in the database table. Table Structure like: CustRegion, CustName, OrdersPlaced. ...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.