Question Help using SQL

Lightor

New member
Joined
Nov 27, 2012
Messages
2
Programming Experience
1-3
I'm a bit new to C# and brand new to SQL. I've been playing around with Visual Studio 2010 for a few weeks now and my few years of C++ back in my college years makes C# not too much of a pain to learn. I've run into a bit of a problem now though, my knowledge of SQL is near none. Let me try to explain what I'm trying to do firstI'm writing a program to help me at work. At my job we have many (~100) tools called PModules and each has a bunch of information tied to them. Now at first my basic understanding of programming got me thinking I could make a PModule class and make a ton of PModule objects. Something along the lines of:

public PModule(string iSN, string iLoc, int iNumChambers)
{
string SN = iSN;
string loc = iLoc;
int numChambers = iNumChambers;
}


PModule J01 = new PModule("H0125", "Bay C, 3rd PM on the right", 4);


PModule J06 = new PModule("H0173", "Bay D, 1st PM on the left", 6);


Now I've looked into it and realized I can have an SQL database with columns that hold all this info. I've watched a dozen YouTube videos and read a bunch of tutorials but I can't figure out how to do exactly what I'm trying to do, they all seem to dump the whole database on a form. All I've basically figured out from these that helps me is how to make a database. What I want to try to do is search the database, which I'm guessing I could do with a loop For Loop. Say I want to find PModule J01...

for (int i = 1; i <= lenghtOfDB; i++)
{
if (db.id(i).PModuleName == "J01")
{
found = true;
break;
}
}


Now I'm not sure how to get the length of the database, which would be the condition as it counts through the IDs, then however you call that column that has the names and would check it till I found it. Once I found it I would like to make, say, textBox1.text = the number of chambers.
In the end I'm looking at making a drop down that has all these PMs (J01, J02, J03) then when they select one, they can hit a button that says Get Number of Chambers and a textBox will be set to have that number. Sorry for my ignorance and long winded'ness here but I'm at my wits end and I wanna make sure I cover all my bases. Thanks a ton for any information that can help me out here.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,279
Location
Sydney, Australia
Programming Experience
10+
Databases don't have a length and there's no looping involved. The whole point is that you write a SQL query and send it to the database to be executed and it sends you back the results. In your case the query would be:
C#:
SELECT * FROM PModule WHERE PModuleName = 'J01'
You'll actually want a query that can searched for any name, so the query would be:
C#:
SELECT * FROM PModule WHERE PModuleName = @PModuleName
and then you can set a value for the @PModuleName parameter. There's lots of information around about using ADO.NET but one place you might start is the Data Walkthroughs link in my signature.
 
Top Bottom