Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

So here i am asking a question once again! Okay so my question is how do i read data from multiple sql tables if i only know the column names. EG.

eqcas database

[User Info] - cas_user_ext - eqcas
Email adress - "email"
Full Name - "fullname"

[User Pin] - cat_validation - eqcas
Name - "name"
Primary Pin - "primarypin"
Secondary Pin - "secondarypin"

cas_user_ext (table)
cat_validation (table)

"text" (the column names)

C#
    public EQData()
    {
        SQLConnect sqlcon = new SQLConnect();
        SqlDataReader myReader = null;
        //SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname",
                                                                                //sqlcon.Connection);
        SqlCommand myCommand = new SqlCommand("select x_id, fullname, email, name, primarypin, secondarypin FROM cas_user_ext, cat_validation WHERE name = @name",
                                                                    sqlcon.Connection);
        myCommand.Parameters.AddWithValue("@name", "ivan1");
        myReader = myCommand.ExecuteReader();
        while (myReader.Read())
        {
            x_id = myReader["x_id"].ToString();
            fullname = myReader["fullname"].ToString();
            email = myReader["email"].ToString();
            name = myReader["name"].ToString();
            primarypin = myReader["primarypin"].ToString();
            secondarypin = myReader["secondarypin"].ToString();
        }
        sqlcon.Close();
    }
}


and also i'm using a class to get the info (the above is part of my class)



So lets say i want to retrieve data from from both tables for all the columns, but the only info i have is the users "name". How would i achieve that?

When i run my application i am retrieving the correct information for cas_user_ext table but not for cat_validation, cat_validation retrieve a different users information.

Thanks in advance
Posted
Comments
Ivan Lubbe 15-Sep-14 5:20am    
I have also noticed that the x_id column in cas_user_ext is called id in the cat_validation but they have the same values.
Prasad Avunoori 15-Sep-14 5:48am    
I didn't get you. Can please post your table name along with their columns and relation?
Ivan Lubbe 15-Sep-14 6:22am    
Okay so i have two tables.

cat_validation
and
cas_user_ext

the columns for cat_validation are name, id, primarypin and secondarypin

the columns for cas_user_ext are fullname, x_id, email

now i want to return all columns data for both tables for the 'name' i have

Now, in order to make a query that makes sense, you need to know the database structure. That's a must.

When you know the table names as you have listed in your query you typically need a join to correctly join rows in both tables. Have a look at join definitions from http://en.wikipedia.org/wiki/Join_(SQL)[^]
 
Share this answer
 
You'll need to join the cat_validation id with cas_user_ext xid.

Try this sql:
select ue.x_id, ue.fullname, ue.email, ct.name, ct.primarypin, ct.secondarypin from cat_validation as ct join cas_user_ext as ue on ct.id = ue.x_id where ct.name = @name
 
Share this answer
 
Okay, so i have managed to get a little further by finally finding some info that helps.

C#
SqlCommand myCommand = new SqlCommand("select x_id, fullname, email, name, primarypin, secondarypin FROM cas_user_ext, cat_validation WHERE x_id = @key AND id = @key",
                                                            sqlcon.Connection);
myCommand.Parameters.AddWithValue("@key", "501");


So i have managed to retrieve the correct data from both tables. The problem i have now is how do i retrieve the key.id for lets say the name column (assuming i have the username of the user only)
 
Share this answer
 
Comments
Ivan Lubbe 15-Sep-14 7:00am    
How would i use scope identity in my above code? I assume this is what i should use to retrieve the key?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900