943,861 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3620
  • C# RSS
Sep 25th, 2009
0

dynamic sql result into array

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. aResult = functionsMysql.getArray(query);
  2.  
  3.  
  4. class functionsMysql
  5. {
  6.  
  7. function getArray (string query)
  8. {
  9.  
  10. ...connect and execute query
  11. while(Reader.Read())
  12. {
  13. ...fill...
  14. }
  15.  
  16. }
  17.  
  18. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
DeOiD is offline Offline
30 posts
since Apr 2007
Sep 25th, 2009
1

Re: dynamic sql result into array

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)
  1. public void BuildDynamicTableArrayExample()
  2. {
  3. DataTable dt = new DataTable(); // table to hold each record
  4. dt.Columns.Add(new DataColumn("FieldName1", typeof(string))); // Create a string column dynamically
  5. dt.Columns.Add(new DataColumn("FieldName2", typeof(int))); // create an int column dyn
  6. dt.Columns.Add(new DataColumn("FieldName3", typeof(double))); // create a double column dyn
  7. for (int r = 0; r < 10; r++) // generate 10 rows of data
  8. {
  9. DataRow dr = dt.NewRow(); // create a row object of table's type defined above
  10.  
  11. dr["FieldName1"] = "data " + r; // add some data...
  12. dr["FieldName2"] = r;
  13. dr["FieldName3"] = (double)r;
  14.  
  15. dt.Rows.Add(dr); // add the row to the table
  16. }
  17. }
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 25th, 2009
2

Re: dynamic sql result into array

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:
c# Syntax (Toggle Plain Text)
  1. public class Result
  2. {
  3. int rowNumber;
  4. string fieldName;
  5. string value;
  6.  
  7. public Result(int rorNumber, string fieldNumber, string value)
  8. {
  9. // assign variables
  10. }
  11. }

if you have a few types of results, you can create more classes/structures ...
c# Syntax (Toggle Plain Text)
  1. public class Result2
  2. {
  3. int rowNumber;
  4. string fieldName;
  5. string value;
  6. string whatever;
  7.  
  8. public Result2(int rorNumber, string fieldNumber, string value, string whatever)
  9. {
  10. // assign variables
  11. }
  12. }

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)
  1. List<object> resultSet = new List<object>();
  2. .. or
  3. List<SomeBaseClass> resultSet = new List<SomeBaseClass>();

and add elements to it:
c# Syntax (Toggle Plain Text)
  1. resultSet.Add(new Result(1, "someName", "someValue"));
  2. resultSet.Add(new Result2(1, "someName", "someValue", "something else"));
Depending on what way you'll choose - the new element of resultSet would be automatically converted to either Object or SomeBaseClass .

And then to determine, which type of result you currently working with - use is or as keywords.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Sep 25th, 2009
0

Re: dynamic sql result into array

thank's for the replies, i will test these solutions tonight and post my results...
Reputation Points: 10
Solved Threads: 0
Light Poster
DeOiD is offline Offline
30 posts
since Apr 2007
Sep 25th, 2009
0

Re: dynamic sql result into array

You can also use a 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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 25th, 2009
0

Re: dynamic sql result into array

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:

C# Syntax (Toggle Plain Text)
  1. public class FieldValues
  2. {
  3. public int Row { get; set; }
  4. public string FldName { get; set; }
  5. public object FldRowValue { get; set; } // don't know its type yet
  6.  
  7. public FieldValues(int row, string fldName, object value)
  8. {
  9. Row = row;
  10. FldName = fldName;
  11. FldRowValue = value;
  12. }
  13. }
  14. List<FieldValues> fieldValues = new List<FieldValues>();
  15.  
  16. public void AddAnItem(int row, string fldName, object value)
  17. {
  18. fieldValues.Add(new FieldValues(row, fldName, value));
  19. }

DeOiD, be sure to check back periodically for some other's opinions too.

Cheers!
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 26th, 2009
0

Re: dynamic sql result into array

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
Reputation Points: 10
Solved Threads: 0
Light Poster
DeOiD is offline Offline
30 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How To Close All Child Forms In MDI Form: C#.net
Next Thread in C# Forum Timeline: Adding Row Into Datagrid View At Runtime





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC