User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 391,550 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,565 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 3245 | Replies: 3
Reply
Join Date: Apr 2004
Location: Tracy
Posts: 743
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Solution HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object

  #1  
Nov 19th, 2006
Okay, so currently i am working on a final project at my college, i have been tasked with creating a database administration tool.

One of my major hurdles was how to query the MySQL database and return all of the results into a DataGridView object with correct column names.

As of right now it will not use the correct data types, all data inputed into the cells will be assumed default textboxcell, but i am working on that and maybe my application will contain a future update that take into account the data type of the cell.

but here we go, this is only how to do it. i have not included any information on how to contact your server as i assume you have some prior C# knowledge of connecting to sql based servers.

please note that i am working in visual studio C# express and that i am using .NET Framework 2.0
/*
     a possible query to run on the database
*/
     //using System.Data.Odbc;
     OdbcCommand odbcCom = new OdbcCommand();
     odbcCom.Connection = <some odbcConnection to SQL server>;
     odbcCom.Command = "select distinct user, host, grant_priv from mysql.user where usere like '%foo%' and grant_priv = 'N';";
     OdbcDataReader odbcRead = odbcCom.ExecutReader();
 
     dgvBuildMe.Columns.Clear();
/*
     Read the Query into the DGV
*/
     DataGridViewColumn colHead;//create the column
     DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();  //apply the default textbox column style
     DataGridViewCell cell = new DataGridViewTextBoxCell(); //apply the default cell style
     int intDgvCount = odbcRead.FieldCount; //set the counter to the number of fields
     int intDgvMin = 0; //starting point
     while (intDgvMin >= 0 && intDgvMin < intDgvCount)
     {
          colHead = new DataGridViewColumn();  //establish new column
          colHead.Name = odbcRead.GetName(intDgvMin) as string; //get the correct name for the column
          colHead.CellTemplate = cell; //apply the cell template to use
          dgvBuildMe.Columns.Add(colHead); //add the collumns
          intDgvMin++;
     }
     intDgvMin = 0; //reset counter
     string[] holder = new string[intDgvCount]; //establish a holder for the query
     while (odbcRead.Read())
     {
         while (intDgvMin >= 0 && intDgvMin < intDgvCount)
         {
              holder[intDgvMin] = odbcRead.GetString(intDgvMin); //build the row off of the current data.
              if (intDgvMin == (intDgvCount - 1))
              {
                   dgvBuildMe.Rows.Add(holder); //if it is the last item in the current row then go ahead and add the holder
              }
         intDgvMin++;
        }
        intDgvMin = 0;
     }

i really hope that this helps someone as much as it has helped me. this should return the results of a query no matter how large the query is. 100 columns or a 1000 rows it shouldnt matter.

i have not tested it on that scale yet but it should work!
Last edited by Killer_Typo : Nov 19th, 2006 at 2:36 pm. Reason: some typos.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 743
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object

  #2  
Nov 21st, 2006
well i do hope this helps someone :lol:
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Join Date: Nov 2006
Location: Denmark
Posts: 2
Reputation: Hoygaard is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Hoygaard Hoygaard is offline Offline
Newbie Poster

Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object

  #3  
Nov 26th, 2006
Thanks dude, it's exactly what I've been looking for
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 743
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object

  #4  
Nov 26th, 2006
Originally Posted by Hoygaard View Post
Thanks dude, it's exactly what I've been looking for


awesome, glad i was able to help someone out.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C# Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 9:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC