| | |
dynamic sql result into array
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 30
Reputation:
Solved Threads: 0
Hi
I want to fetch results from a given table (in this case using MySql) and put them in a array or arrayList or List<> (whatever works better). The thing is that this must be made dynamically. The idea is something like this:
I already have the connection working and the query is executed but i don“t have any idea how to put the results into the array, because the result doesn't a fixed number of rows or fields.
The array(or whatever...) has to be something like:
aResult[RowNumber, FieldName, Row.Field.Value]
is this possible?
Needless to say I'm a C# beginner and English isn't my language
I want to fetch results from a given table (in this case using MySql) and put them in a array or arrayList or List<> (whatever works better). The thing is that this must be made dynamically. The idea is something like this:
C# Syntax (Toggle Plain Text)
aResult = functionsMysql.getArray(query); class functionsMysql { function getArray (string query) { ...connect and execute query while(Reader.Read()) { ...fill... } } }
I already have the connection working and the query is executed but i don“t have any idea how to put the results into the array, because the result doesn't a fixed number of rows or fields.
The array(or whatever...) has to be something like:
aResult[RowNumber, FieldName, Row.Field.Value]
is this possible?
Needless to say I'm a C# beginner and English isn't my language
Last edited by DeOiD; Sep 25th, 2009 at 10:42 am.
•
•
Join Date: Jul 2009
Posts: 908
Reputation:
Solved Threads: 145
Here is an example of how you could build a dynamic table to hold your data. The field names and data types are up to you to decide, etc.:
C# Syntax (Toggle Plain Text)
public void BuildDynamicTableArrayExample() { DataTable dt = new DataTable(); // table to hold each record dt.Columns.Add(new DataColumn("FieldName1", typeof(string))); // Create a string column dynamically dt.Columns.Add(new DataColumn("FieldName2", typeof(int))); // create an int column dyn dt.Columns.Add(new DataColumn("FieldName3", typeof(double))); // create a double column dyn for (int r = 0; r < 10; r++) // generate 10 rows of data { DataRow dr = dt.NewRow(); // create a row object of table's type defined above dr["FieldName1"] = "data " + r; // add some data... dr["FieldName2"] = r; dr["FieldName3"] = (double)r; dt.Rows.Add(dr); // add the row to the table } }
Hello, DeOiD.
Here's one more way to go ..
If choose between Array, ArrayList or List<T> - I would suggest you use List<T>.
For keeping such values you can define a class or structure. For example:
if you have a few types of results, you can create more classes/structures ...
We know, that List can contain the elements of same type. So we should find the connection between 2 created classes:
1. Both classes (also as all reference types) are inherit from System.Object.
2. You can define the parent class for them by yourself.
What it will give to you? After that you can create a List:
and add elements to it:
Depending on what way you'll choose - the new element of resultSet would be automatically converted to either
And then to determine, which type of result you currently working with - use
Here's one more way to go ..
If choose between Array, ArrayList or List<T> - I would suggest you use List<T>.
For keeping such values you can define a class or structure. For example:
c# Syntax (Toggle Plain Text)
public class Result { int rowNumber; string fieldName; string value; public Result(int rorNumber, string fieldNumber, string value) { // assign variables } }
if you have a few types of results, you can create more classes/structures ...
c# Syntax (Toggle Plain Text)
public class Result2 { int rowNumber; string fieldName; string value; string whatever; public Result2(int rorNumber, string fieldNumber, string value, string whatever) { // assign variables } }
We know, that List can contain the elements of same type. So we should find the connection between 2 created classes:
1. Both classes (also as all reference types) are inherit from System.Object.
2. You can define the parent class for them by yourself.
What it will give to you? After that you can create a List:
c# Syntax (Toggle Plain Text)
List<object> resultSet = new List<object>(); .. or List<SomeBaseClass> resultSet = new List<SomeBaseClass>();
and add elements to it:
c# Syntax (Toggle Plain Text)
resultSet.Add(new Result(1, "someName", "someValue")); resultSet.Add(new Result2(1, "someName", "someValue", "something else"));
Object or SomeBaseClass .And then to determine, which type of result you currently working with - use
is or as keywords. So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
You can also use a 
DdoubleD/Antenka showed great ways to go about it.
HashTable to access the columns by names. I think that is along the lines of what you may want if the other (better!) solutions don't pan out 
DdoubleD/Antenka showed great ways to go about it.
•
•
Join Date: Jul 2009
Posts: 908
Reputation:
Solved Threads: 145
That's how I originally started thinking it as an example, then I scrapped it figuring it wasn't practical for using the list, but don't really know exactly how it will be used anyway:
DeOiD, be sure to check back periodically for some other's opinions too.
Cheers!
C# Syntax (Toggle Plain Text)
public class FieldValues { public int Row { get; set; } public string FldName { get; set; } public object FldRowValue { get; set; } // don't know its type yet public FieldValues(int row, string fldName, object value) { Row = row; FldName = fldName; FldRowValue = value; } } List<FieldValues> fieldValues = new List<FieldValues>(); public void AddAnItem(int row, string fldName, object value) { fieldValues.Add(new FieldValues(row, fldName, value)); }
DeOiD, be sure to check back periodically for some other's opinions too.
Cheers!
•
•
Join Date: Apr 2007
Posts: 30
Reputation:
Solved Threads: 0
Hi
I've tested Antenka's solution and it works like a charm. Thank you very much.
DdoubleD solution apparently works too, but i haven't had the time to test it yet. It seems to me that, for what i want, it only needs another loop to dynamically set the fieldName and its value...
Thank so much for your replies, now i have at least 2 working solutions for my problem
I've tested Antenka's solution and it works like a charm. Thank you very much.
DdoubleD solution apparently works too, but i haven't had the time to test it yet. It seems to me that, for what i want, it only needs another loop to dynamically set the fieldName and its value...
Thank so much for your replies, now i have at least 2 working solutions for my problem
![]() |
Similar Threads
- Populating a datagrid with dynamic SQL (ASP.NET)
- Dynamic SQL - Need help (Oracle)
- vb.net dynamic multi dimention array (VB.NET)
- How to find the UserID on logging time (PHP)
- From SQL string to array (PHP)
- How to covert this sql statement to array (PHP)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- Javascript array from sql query (JSP)
- creating dynamic textfields (Java)
Other Threads in the C# Forum
- Previous Thread: How To Close All Child Forms In MDI Form: C#.net
- Next Thread: Adding Row Into Datagrid View At Runtime
| Thread Tools | Search this Thread |
.net access algorithm alignment array barchart bitmap box broadcast buttons c# c#gridviewcolumn check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing elevated encryption enum event excel file focus forloop form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml






