Dynamic Members c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rottmanj
    New Member
    • Jan 2008
    • 46

    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.Var Char); // static way to add

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

    Code:
                for (int i = 0; i < (fldCol[0] as ArrayList).Count; i++)
                {
                    string fldName = "@" + (fldCol[0] as ArrayList)[i].ToString();
                    string fldType = (fldCol[1] as ArrayList)[i].ToString();
                    fldType = fldType .Replace("(", "");
                    fldType = fldType .Replace(")", "");
    
                    cmd.Parameters.Add(fldName, MySqlDbType[fldType]);
    
                    cmd.Parameters[fldName].Value = dataCol[i];
                }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Have you considered using cmd.Parameters. AddWithValue() instead?
    Then you would say:
    cmd.Parameters. AddWithValue(My ParamName,MyPar amValue);

    It determines the correct sql type from the object type passed to MyParamValue

    Comment

    • rottmanj
      New Member
      • Jan 2008
      • 46

      #3
      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.

      Comment

      • rottmanj
        New Member
        • Jan 2008
        • 46

        #4
        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.

        Code:
                                        if (customer != null || fldArr[k].ToString() != null)
                                        {
                                            string qbFld = fldArr[k].ToString();
        
                                            dataCol.Add(customer.qbFld.GetValue().ToString());
        
                                        }

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Are the fields being returned as parameters or as a dataset?

          Comment

          • rottmanj
            New Member
            • Jan 2008
            • 46

            #6
            They are being returned as a dataset in an ArrayList.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              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

              Comment

              • rottmanj
                New Member
                • Jan 2008
                • 46

                #8
                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.

                Code:
                                                if (customer != null || fldArr[k].ToString() != null)
                                                {
                                                    string qbFld = fldArr[k].ToString();
                
                                                    dataCol.Add(customer.dbFld.GetValue().ToString());
                
                                                }

                Comment

                • rottmanj
                  New Member
                  • Jan 2008
                  • 46

                  #9
                  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(cus tomer.FirstName .GetValue().ToS tring());

                  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.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Can you create the array with the 'column name' AS the index?
                    Like a Dictionary<stri ng, object>?

                    Comment

                    • rottmanj
                      New Member
                      • Jan 2008
                      • 46

                      #11
                      Yes I can, I wind up with something like below, just not sure what to do with it.

                      FirstName,strin g
                      LastName,string

                      Comment

                      Working...