Thread: Help with passing data base information into my class methods!!!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Unhappy Help with passing data base information into my class methods!!!

    Hi guys. I'm new to C# though I have programming experiences with other languages. My question seems too noobie but the true is that I was raised programming at low level. This high level things are new for me so... sorry if this is really easy.

    What i'm trying to do is to pass some information from a data base via parameters to my class method. Here's my class:

    Code:
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace Relationship_Management
    {
        public class Individual
        {
           
            private int id;
            private string name;
            private string lastName;
            private string existingRelation;
            private string microsoftFriendly;
            private string hasBudget;
            private string underRadar;
            private int reportTo;
    
            public int ID
            {
                get { return id; }
                set { id = value;}
            }
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public string LastName
            {
                get { return lastName; }
                set { lastName = value; }
            }
    
            public string ExistingRelation
            {
                get { return existingRelation; }
                set { existingRelation = value; }
            }
    
            public string MicrosoftFriendly
            {
                get { return microsoftFriendly; }
                set { microsoftFriendly = value; }
            }
    
            public string HasBudget
            {
                get { return hasBudget; }
                set { hasBudget = value; }
            }
    
            public string UnderRadar
            {
                get { return underRadar; }
                set { underRadar = value; }
            }
    
            public int ReportTo
            {
                get { return reportTo; }
                set { reportTo = value; }
            }
    
        }
    }
    So when I go to main, I can do something like this:

    Code:
     Individual person = new Individual();
    
                person.ReportTo = 345;
    Now, that number is suppose to be a random ID number that a DBMS generate automatically. My question is this... HOW IN THE WORLD can I do to pass fields of the data base or any info. of a table to my object fields? I mean something like this:

    Code:
    Individual person = new Individual();
    
               // person.ReportTo = TableName.ID (data base info);
    Or there is a way via query to assing a data base value to a variable? I hope that I explained myself well enough and sorry about my english. Is not my main language. I will waiting for your answers people.

    Many thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You want function (method) arguments:
    Code:
    public InitializeIndividual( int anId, string aFirstName, string aLastName, string anExistingRelation, ... )
    {
            id = anId;
            name = aFirstName;
            lastName = aLastName;
            existingRelation = an ExistingRelation;
            ...
    }
    ...
    Foo Bar = new Foo();
    Bar.InitializeIndividual( 1, "Bob", "Barker", ... );
    C# 4.0 - Lesson 07: Introduction to the Methods of a Class


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Unhappy Hmmm...

    That would be a constructor and for some cases I preffered to create a new get and set for each of them. I really appreciate your fast answer but my question is not about how I have to build my class. My question is about database. How do I use database fields and convert them in to arguments for my method. Even using your suggestion, is the same for me. What you wrote:

    Code:
    public InitializeIndividual( int anId, string aFirstName, string aLastName, string anExistingRelation, ... )
    {
            id = anId;
            name = aFirstName;
            lastName = aLastName;
            existingRelation = an ExistingRelation;
            ...
    }
    ...
    Foo Bar = new Foo();
    Bar.InitializeIndividual( 1, "Bob", "Barker", ... );
    It's just the same. I just want this part not been hard coded:

    Code:
    Bar.InitializeIndividual( 1, "Bob", "Barker", ... );
    I want that, besides of "Bob", "Barker" and that kinds of hard coded arguments, I want to pass database fields through it. Example:"

    Code:
    Bar.InitializeIndividual( 1, "Table.Field X, "variableMadeFromDataBaseField", ... );
    Is that even possible? It has to be. I repeat, many thanks but I don't want to write my arguments. I want to take them from a data base. How I do that?????

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Exclamation I think I found in Google what I was looking for BUT....

    I think I found what I was looking for in Google but damn... it seems kinda complicated. Would someone try to explain me in english at least?

    Here's the link: How To Use A Database In Your Program Part II - .NET Framework

    Waiting for your answers. Thank you again

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on your database. I assume you have some functionality provided by it, some API or something you use to fetch and send things? Well, use whatever it is that fetches information to do so, and convert the strings (or whatever it actually gives you) into something you can work with. I mean if I were using MySQL, I'd go read up on MySQL API for whatever language I was trying to make use of it in. You are going to have to do the same here. I don't know much about C#, but I'd be surprised if there was some generic catch-all database hook you could use.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Thumbs up Hmm...

    Well man I will go for it. I will give a try to an specific DBMS API. I will post any update. If there's any other way you could help me you are most welcome. And a thousand thanks man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-07-2011, 09:45 AM
  2. Calling base class function when base is a template
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2010, 02:26 AM
  3. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  4. Replies: 5
    Last Post: 10-28-2006, 11:27 AM
  5. Not 100% sure when to use virtual methods in base class
    By Silvercord in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 03:19 PM

Tags for this Thread