473,574 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Com mon;

using System.Data.Sql Client;

using System.Data.Sql Types;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

int XXX = 0;
string strConnection = @"Data
Source=.\SQLEXP RESS;AttachDbFi lename=D:\CSRBC \rbc\dbEnum2BLK .mdf;Integrated
Security=True;C onnect Timeout=30;User Instance=True";

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

SqlConnection oConnection = new SqlConnection(s trConnection);

SqlCommand oCommand = new SqlCommand(strC ommand, oConnection);

oCommand.Connec tion.Open();

XXX = oCommand.Execut eNonQuery();

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

Console.ReadLin e();

oCommand.Connec tion.Close();
}

}

}
Dec 4 '05 #1
3 1492

"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:Zu******** ***********@wag ner.videotron.n et...
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.reco rdsaffected is -1. How come?

using System;

using System.Data;

using System.Data.Com mon;

using System.Data.Sql Client;

using System.Data.Sql Types;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXP RESS;AttachDbFi lename=D:\CSRBC \rbc\dbEnum2BLK .mdf;Integrated
Security=True;C onnect Timeout=30;User Instance=True";

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

SqlConnection oConnection = new SqlConnection(s trConnection);

SqlCommand oCommand = new SqlCommand(strC ommand, oConnection);

oCommand.Connec tion.Open();

SqlDataReader myReader;

myReader = oCommand.Execut eReader();

try

{

while (myReader.Read( ))

{

Console.WriteLi ne(myReader.Rec ordsAffected);

}

}

finally

{

myReader.Close( );

oConnection.Clo se();

}

Console.ReadLin e();

}

}

}
"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:Zu******** ***********@wag ner.videotron.n et...
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.Com mon;

using System.Data.Sql Client;

using System.Data.Sql Types;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

int XXX = 0;
string strConnection = @"Data
Source=.\SQLEXP RESS;AttachDbFi lename=D:\CSRBC \rbc\dbEnum2BLK .mdf;Integrated
Security=True;C onnect Timeout=30;User Instance=True";

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

SqlConnection oConnection = new SqlConnection(s trConnection);

SqlCommand oCommand = new SqlCommand(strC ommand, oConnection);

oCommand.Connec tion.Open();

XXX = oCommand.Execut eNonQuery();

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

Console.ReadLin e();

oCommand.Connec tion.Close();
}

}

}

Dec 4 '05 #3

"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:2k******** ***********@wag ner.videotron.n et...
I also tried this, but the value that is returned by the
datareader.rec ordsaffected is -1. How come?

using System;

using System.Data;

using System.Data.Com mon;

using System.Data.Sql Client;

using System.Data.Sql Types;

using System.Text;

using System.IO;

namespace TestSelect

{

class Program

{

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXP RESS;AttachDbFi lename=D:\CSRBC \rbc\dbEnum2BLK .mdf;Integrated
Security=True;C onnect Timeout=30;User Instance=True";

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

SqlConnection oConnection = new SqlConnection(s trConnection);

SqlCommand oCommand = new SqlCommand(strC ommand, oConnection);

oCommand.Connec tion.Open();

SqlDataReader myReader;

myReader = oCommand.Execut eReader();

try

{

while (myReader.Read( ))

{

Console.WriteLi ne(myReader.Rec ordsAffected);

}

}

finally

{

myReader.Close( );

oConnection.Clo se();

}

Console.ReadLin e();

}

}

}

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
2740
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 words? How about exact phrases? I saw a solution where all words are preindexed in a "dictionary" like table and then another table stores the word...
8
1562
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 represents a customer. As customers come and go, so tables will be added and removed. I need to create a search utility that will search all tables...
33
2457
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 criteria: I have a list of about 100 numbers which correspond to the ID field and also another 40 say numbers corresponding to the numbers in the...
5
2386
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 three bolded periods '...' in search-result listings. Additionally, most content is outdated; as many users need up-to-date content. Hence,...
7
4531
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 records. Should I design a new form for this or can I somehow make this work in the same form. Thanks in advance, john
3
3276
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 "Searching..." page then, refreshes and displays the table I want. I have code that grabs data from the page using cURL but when I look at the data it contains...
3
7343
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 or DB2 UDB Text Information Extender for searching CLOBS but what are the other solutions out there. can i use some other data type and create index...
12
2363
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 registry mark given by underwriters (as at Lloyd's) to ships in first-class condition. Inferior grades are indicated by A 2 and A 3. () The first...
20
2513
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. Aslo, the tables for drawings are grouped by year. I have a Main Form and on that main form, several cmd buttons to initialize sub forms are...
3
2431
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. One of the customers has a name like O'Neil Me.lblOrderCount.Caption = "Total Orders Placed: " & DCount("CustName", "backUpData", " = '" &...
0
7758
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...
0
8270
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7856
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...
0
6509
agi2029
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...
0
5335
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...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2270
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
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
bsmnconsultancy
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...

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.