Exporting selected ArrayList values to SQL Server 2000

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • R2d2Rabeau
    New Member
    • May 2007
    • 21

    Exporting selected ArrayList values to SQL Server 2000

    Hi there,

    I Have an arraylist that holds both strings and integers and I was wondering what was the simplest way to export selected elements from it to a SQL Server 2000 table?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Loop through it?
    You are going to need to be more specific then that. We need details :-)

    Comment

    • R2d2Rabeau
      New Member
      • May 2007
      • 21

      #3
      Hi,

      Ok, so I loop through it, using IEnumerator? Then shall I create a temporary datatable where the data will be sent to, just before being imported to the sql database?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        ArrayLists can be accesed with []
        Code:
        //if you have mixed content
        for (int i=0;i<myarraylist.Count;i++)
        {
           // do something with myarraylist[i]
           if (myarraylist[i].GetType()==typeof(string))
           {
              string myval=(string)myarraylist[i]);
           }
           if (myarraylist[i].GetType()==typeof(int))
           {
              int myval=(int)myarraylist[i]);
           }
        }
        
        //if content is all of same type
        foreach(string mystring in myarraylist)
        {
           // do something with mystring
        }

        Comment

        • R2d2Rabeau
          New Member
          • May 2007
          • 21

          #5
          Thanks a lot Plater!
          I never thought of doing it that way.

          What about the export bit? Is using a temporary datatable the most effective solution?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            use SQL's INSERT and UPDATE to stick them into a table.

            Check How to use a Database in your program for more information on databasing

            Comment

            • R2d2Rabeau
              New Member
              • May 2007
              • 21

              #7
              Thanks for the link.

              In the exemple given the values are included in the code:

              Code:
              sqlCom.Connection = dbCon
              sqlcom.CommandType = CommandType.Text
              sqlCom.CommandText = "INSERT INTO CLIENT_TABLE(cID, cAddress, cPhoneNumber)" _
              " VALUES(@cID,@cAddress,@cPhoneNumber)"
              sqlCom.Parameters.Add("@cID", SqlDbType.Int).Value = "[U]1234567[/U]"
              sqlCom.Parmaeters.Add("@cAddress", SqlDbType.VarChar).Value = "[U]123 Hypoth. Street.[/U].."
              sqlCom.Parameters.Add("@cPhoneNumber",SqlDbType.VarChar.Value="[U]1-800-999-99999[/U]"
              What is the cleanest way to use values from an arrayList directly?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Just use the values in place of the hardcoded ones?
                Code:
                sqlCom.Connection = dbCon
                sqlcom.CommandType = CommandType.Text
                sqlCom.CommandText = "INSERT INTO CLIENT_TABLE(cID, cAddress)" _
                " VALUES(@cID,@cAddress)"
                sqlCom.Parameters.Add("@cID", SqlDbType.Int).Value = myarraylist[i].ToString(); 
                sqlCom.Parmaeters.Add("@cAddress", SqlDbType.VarChar).Value = myarraylist[i+1].ToString();

                Comment

                • R2d2Rabeau
                  New Member
                  • May 2007
                  • 21

                  #9
                  Thanks a lot Plater, you solved my problem from A to Z, I appreciate it.
                  Have a good weekend.

                  God Bless.

                  Comment

                  • oohay251
                    New Member
                    • May 2007
                    • 27

                    #10
                    peruse the System.Data.Dat aSet
                    and System.Data.Sql Client.SqlDataA dapter classes as well.

                    Comment

                    Working...