Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple tables none of them is related to each other.

I want to select data from database and display it on the form when form loads for the first
time. using ms access as backend

There are different parameters, text , listboxes ,tooltips, Language selection, which are filled from database when form loads.
I am using background thread for this so no GUI issues.

But Is it right to write the select statement in one function as below.

C#
void LoadDatafromDatabase()
{

try{

 OleDbConnection AccessConnection = new OleDbConnection(@"Provider = Microsoft.JET.OLEDB.4.0;data source =" + Application.StartupPath + "\\" + "FileDatabase.mdb;Jet OLEDB:Database password =;);

               AccessConnection.Open();

 //select query 1
    OledbCommand AccessCommand = new OleDbCommand("select Value from Parameters ",AccessConnection);
               DataReader = AccessCommand.ExecuteReader();
               
               int j=0;
               while (DataReader.Read())
               {
                   FormValue = DataReader.GetValue(1).ToString();
                   LoadFormValues[j] = FormValue;
                   j++;
               }        
//select query 2
 OledbCommand AccessCommand = new OleDbCommand("select Value from Tooltip",AccessConnection);
               DataReader = AccessCommand.ExecuteReader();
               
               int j=0;
               while (DataReader.Read())
               {
                   FormValue = DataReader.GetValue(1).ToString();
                   FormTooltip[j] = FormValue;
                   j++;
               }        

////select query 3...n
         .........      like this I have used different select queries for different 
   tables.

}

catch (Exception ex)
            {
               //catch exp
            }
            finally
            {
           //dispose open connenction ,datareader
            }
}


Is it a good coding practice
Posted

While it is possible to get back multiple datatables from 1 query into a dataset, the only time I tested this it was dismally slow, it was some years ago and I have never repeated the experiment.

I was attempting to pull in a number (6) of very small tables to support the lookup function in an application.
 
Share this answer
 
Comments
krishpraj123 20-May-14 4:04am    
My tables are totally different. Only ID and Values column in tables.How I am going to join them and if yes, on which basis.
Can u please suggest me how i can solve this.
can you give me example
Your approach should look like following

Step 1: merge all sql queries as one. For example
C#
sql query = "select Value from Parameters;select Value from Tooltip;";


Step 2: Execute the query and read multiple resultset

C#
using (OledbCommand command = new OledbCommand
       ("select Value from Parameters;select Value from Tooltip;", AccessConnection ))
{
    using (DataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            MessageBox.Show(reader.GetString(0), "Table1.Column1");
        }

        if(reader.NextResult())
        {
           while (reader.Read())
          {
            MessageBox.Show(reader.GetString(0), "Table2.Column2");
          }
        }
    }
}
 
Share this answer
 
Comments
krishpraj123 21-May-14 3:57am    
Thank you for reply sir,
but for third table does I have to use reader.NextResult()) again.
and does I need to use dataset or datareader instead of reader.read(); for
performance.

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