| | |
HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object
0
#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
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!
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 3:36 pm. Reason: some typos.
Dont forget to spread the reputation to those that deserve!
Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object
0
#2 Nov 21st, 2006
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object
0
#3 Nov 26th, 2006
Re: HOWTO: Run a Query on a Database using ODBC and return all Results into a DGV Object
0
#4 Nov 26th, 2006
![]() |
Similar Threads
- "Expired Members" (Social Media and Online Communities)
- using foreach with multi-dimensional arrays (PHP)
- HOWTO: Share an SQL Connection between multiple forms within the same project (C#)
- session_start(): Cannot send session cache limiter - (PHP)
- session_start(): Cannot send session cache limiter - (PHP)
Other Threads in the C# Forum
- Previous Thread: pls help me with my card game
- Next Thread: Problem with Windows Service
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





