Page 1 of 1

query data from multiple tables

Posted: Fri Sep 28, 2012 4:41 pm
by omayer
Would you please tell me how to query data from multiple tables , Here is the code that I am using for select from one table but can't figured it out how to do from multiple tables. Thank you in Advance

private SqlConnection _conn;
private SqlParameter paramRunID;
private SqlCommand cmdRunId;

|Constructor|

paramRunID = new SqlParameter();
cmdRunId = new SqlCommand("SELECT * FROM runcontrol WHERE runid LIKE @runidInputFromConsole",_conn);


Code: Select all

   	 public void CreateHouse(string inputFromConsole)
  
    	
    {
   	 	
    	
       try
     	 	{
     	 		_conn.Open();
     	 		
     	 	}
     	 	catch(Exception e)
     	 	{
     	 		Report.Failure(e.ToString());
     	 	}
     	 	
	 		SqlDataReader myReader = null;	 		
 	  		paramRunID.ParameterName = "@runidInputFromConsole"; 
     	 	                paramRunID.Value = inputFromConsole; 
     	 	                cmdRunId.Parameters.Add(paramRunID);
         	
 			
	 		    myReader = cmdRunId.ExecuteReader();
	 		
			    while(myReader.Read())
			    { 

				    _runID = myReader["runid"].ToString();
				    _scenarioID = myReader["scenarioid"].ToString();

					_description = myReader["description"].ToString();

				 	//pass parameter to read scenarioTable
				    scenarioTable.ReadScnTableForCreatingHouse(_runID,_scenarioID,_description);
				  
				    cmdRunId.Dispose();
					cmdRunId.Parameters.Clear();
					
		 	 
   	 		    }
		
     	 		
			    _conn.Close();
			 	  
        }
        

Re: query data from multiple tables

Posted: Mon Oct 01, 2012 8:24 am
by sdaly
Take a look at joins -

select * from table1 t1
inner join table2 t2 on (t2.t1id = t1.id)


http://www.w3schools.com/sql/sql_join.asp