473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dynamic Members c#

I am working on a class that will allow me to insert data into any mysql table that I pass into the class. For the most part I have everything done except for adding parameters to the sql query.

What I have done is pass an ArrayList of column names/ column types into my method, and then I take this data and parse it out and build my query from there. The one issue I have is, when adding parameters to the query, I am unable to figure out how to take the data I passed in and use it in conjunction with MySqlDbType.

Here is a short snippet
cmd.Parameters.Add(fldName, MySqlDbType.VarChar); // static way to add

Here is my current code base. Just as a short fyi, the array list is a multidimensional arraylist. I know the code below is wrong, just now sure how to use the string fldType as a member of MySqlDbType.

Expand|Select|Wrap|Line Numbers
  1.             for (int i = 0; i < (fldCol[0] as ArrayList).Count; i++)
  2.             {
  3.                 string fldName = "@" + (fldCol[0] as ArrayList)[i].ToString();
  4.                 string fldType = (fldCol[1] as ArrayList)[i].ToString();
  5.                 fldType = fldType .Replace("(", "");
  6.                 fldType = fldType .Replace(")", "");
  7.  
  8.                 cmd.Parameters.Add(fldName, MySqlDbType[fldType]);
  9.  
  10.                 cmd.Parameters[fldName].Value = dataCol[i];
  11.             }
  12.  
Nov 12 '08 #1
10 1727
Plater
7,872 Expert 4TB
Have you considered using cmd.Parameters.AddWithValue() instead?
Then you would say:
cmd.Parameters.AddWithValue(MyParamName,MyParamVal ue);

It determines the correct sql type from the object type passed to MyParamValue
Nov 12 '08 #2
Doh, no I didn't even thing about using AddWithValue.

Now it works the way that I want it to. Thank you for your help.
Nov 12 '08 #3
This does open another question for me though.

At another point in my app, I have to use something similar to my question above.

I have to use a dynamic member in order to pull data.

Here is the code I am testing with.

qbFld is the field name I want to pull from the quickbooks file. Just not sure how to create a dynamic member.

Expand|Select|Wrap|Line Numbers
  1.                                 if (customer != null || fldArr[k].ToString() != null)
  2.                                 {
  3.                                     string qbFld = fldArr[k].ToString();
  4.  
  5.                                     dataCol.Add(customer.qbFld.GetValue().ToString());
  6.  
  7.                                 }
  8.  
Nov 12 '08 #4
Plater
7,872 Expert 4TB
Are the fields being returned as parameters or as a dataset?
Nov 12 '08 #5
They are being returned as a dataset in an ArrayList.
Nov 12 '08 #6
Plater
7,872 Expert 4TB
Well if you know the name of the column you want, you should be able to access it via an index somehow, or pull it from a dataset/datatable directly
Nov 12 '08 #7
Trying to pull it as an index is not really a possibility as the column names indexes are not the same as the indexes in my qb database file.

Trying to pull it directly from the array is more along the lines of what I am looking to do. But not sure how to do that, since the member qbFld does not exist. Ideally (pseudo codeesk) i would look like something below.

Expand|Select|Wrap|Line Numbers
  1.  
  2.                                 if (customer != null || fldArr[k].ToString() != null)
  3.                                 {
  4.                                     string qbFld = fldArr[k].ToString();
  5.  
  6.                                     dataCol.Add(customer.dbFld.GetValue().ToString());
  7.  
  8.                                 }
  9.  
Nov 12 '08 #8
Just to give a better example of what it looks like when it is static.

If I wanted to pull the data from the FirstName field I would use this.

dataCol.Add(customer.FirstName.GetValue().ToString ());

In my arraylist there is a string that contains the fieldname FirstName.

What I am trying to figure out is how to take the data from the array, and convert the string into the member FirstName(and the rest), so that I can add the data to the arraylist dataCol.
Nov 12 '08 #9
Plater
7,872 Expert 4TB
Can you create the array with the 'column name' AS the index?
Like a Dictionary<string, object>?
Nov 12 '08 #10
Yes I can, I wind up with something like below, just not sure what to do with it.

FirstName,string
LastName,string
Nov 12 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shailesh | last post by:
If I'm not mistaken, C++ doesn't have support for dynamic class members. I'm considering if such a facility would be useful, and what method would make a good workaround. I have a generic...
5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
2
by: Caro | last post by:
I have a stored procedure spGetAccessLogDynamic and when I try to call it I get the following error: Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'S'. I dont...
3
by: deejayquai | last post by:
Hi I've created a crosstab query and displayed it as a sub-report in my main report. This is fine until the data changes and the column names become incorrect. I know I have to create a...
3
by: K.S.Liang | last post by:
Hi all, 1> If there are more than one dynamic linking libraries in the file system, how do I know which one is loaded into system? Any C library or system call can tell me which *.so or *.sl is...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
5
by: Andrus | last post by:
How to compile source code into dynamic method ? System.Reflection.Emit.DynamicMethod requires IL code for creation. How to use C# source code to create method instead of manually creating IL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.