Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Database upload and Append

Database upload and Append

Scheduled Pinned Locked Moved Database
csharphelpdatabasevisual-studioquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    L sup2 D
    wrote on last edited by
    #1

    Hi, I am trying to upload a database (which I have no problem doing), then I need to take that database and append it to a master database with the same tables and structure. I am using Access and visual studio .net, C#. I have tried everything, I am new to C# and .net and so maybe I am not putting things in the correct order or something?? Specifically, I have to use two different connections to get to the data, one connection where the data is located (conn1), and then append that data to the master database, (conn2). Please help if you can! :mad::confused: :((

    L²+D

    R 1 Reply Last reply
    0
    • L L sup2 D

      Hi, I am trying to upload a database (which I have no problem doing), then I need to take that database and append it to a master database with the same tables and structure. I am using Access and visual studio .net, C#. I have tried everything, I am new to C# and .net and so maybe I am not putting things in the correct order or something?? Specifically, I have to use two different connections to get to the data, one connection where the data is located (conn1), and then append that data to the master database, (conn2). Please help if you can! :mad::confused: :((

      L²+D

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      Please suppy at least enough code snippets for someone else to understand what you've tried, and which part is not working. Your present question is sufficiently vague that no one is likely to respond.

      L 1 Reply Last reply
      0
      • R Rob Graham

        Please suppy at least enough code snippets for someone else to understand what you've tried, and which part is not working. Your present question is sufficiently vague that no one is likely to respond.

        L Offline
        L Offline
        L sup2 D
        wrote on last edited by
        #3

        Hi Rob, as I said, I am new to this so here is what I have done; private bool uploadData(string myPath, string txtUpload) { bool bNoError = true; string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +myPath +txtUpload +";"; OleDbConnection con = new OleDbConnection(strConnection); con.Open(); try { DataSet dsEWEtmp = new DataSet(); OleDbDataAdapter daEWEForm = new OleDbDataAdapter("SELECT * FROM Some_Form", con); int updates = daEWEForm.Fill(dsEWEtmp, "Some_Form"); foreach (DataRow dataRow in dsEWEtmp.Tables) { dsEWEtmp.Tables[0].ImportRow(dataRow); dsEWEtmp.Tables[0].AcceptChanges(); } updates = daEWEForm.Update(dsEWEtmp); // return dsEWEtmp; DataTable tblEWEForm; tblEWEForm = dsEWEtmp.Tables[0]; OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(daEWEForm); daEWEForm.Update(dsEWEtmp, "Some_Form"); } catch(Exception err) { string str = err.Message; bNoError = false; } finally { if (con.State.ToString() =="Open") { con.Close(); } } return bNoError; As I stated I have the file that I upload; then if there is an uploaded file, it is "supposed" to read that file into a dataset and write it to the master database. In my web.config file, the master database resides at strConnection2, the uploaded file is at strConnection. I am really confused how to access them both, one to get the data from the uploaded file and two connect to the master database and write to it. Is that better? Please help if you can. Thanks!:confused:

        L²+D

        Y R 2 Replies Last reply
        0
        • L L sup2 D

          Hi Rob, as I said, I am new to this so here is what I have done; private bool uploadData(string myPath, string txtUpload) { bool bNoError = true; string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +myPath +txtUpload +";"; OleDbConnection con = new OleDbConnection(strConnection); con.Open(); try { DataSet dsEWEtmp = new DataSet(); OleDbDataAdapter daEWEForm = new OleDbDataAdapter("SELECT * FROM Some_Form", con); int updates = daEWEForm.Fill(dsEWEtmp, "Some_Form"); foreach (DataRow dataRow in dsEWEtmp.Tables) { dsEWEtmp.Tables[0].ImportRow(dataRow); dsEWEtmp.Tables[0].AcceptChanges(); } updates = daEWEForm.Update(dsEWEtmp); // return dsEWEtmp; DataTable tblEWEForm; tblEWEForm = dsEWEtmp.Tables[0]; OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(daEWEForm); daEWEForm.Update(dsEWEtmp, "Some_Form"); } catch(Exception err) { string str = err.Message; bNoError = false; } finally { if (con.State.ToString() =="Open") { con.Close(); } } return bNoError; As I stated I have the file that I upload; then if there is an uploaded file, it is "supposed" to read that file into a dataset and write it to the master database. In my web.config file, the master database resides at strConnection2, the uploaded file is at strConnection. I am really confused how to access them both, one to get the data from the uploaded file and two connect to the master database and write to it. Is that better? Please help if you can. Thanks!:confused:

          L²+D

          Y Offline
          Y Offline
          Youtea
          wrote on last edited by
          #4

          You grab your data from strConnection and stored it in the DataSet dsEWEtmp. Now go ahead and open up strConnection2 and insert the data. I'm pretty sure you can find how to insert from dataset very easily from this site.

          Y 1 Reply Last reply
          0
          • L L sup2 D

            Hi Rob, as I said, I am new to this so here is what I have done; private bool uploadData(string myPath, string txtUpload) { bool bNoError = true; string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +myPath +txtUpload +";"; OleDbConnection con = new OleDbConnection(strConnection); con.Open(); try { DataSet dsEWEtmp = new DataSet(); OleDbDataAdapter daEWEForm = new OleDbDataAdapter("SELECT * FROM Some_Form", con); int updates = daEWEForm.Fill(dsEWEtmp, "Some_Form"); foreach (DataRow dataRow in dsEWEtmp.Tables) { dsEWEtmp.Tables[0].ImportRow(dataRow); dsEWEtmp.Tables[0].AcceptChanges(); } updates = daEWEForm.Update(dsEWEtmp); // return dsEWEtmp; DataTable tblEWEForm; tblEWEForm = dsEWEtmp.Tables[0]; OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(daEWEForm); daEWEForm.Update(dsEWEtmp, "Some_Form"); } catch(Exception err) { string str = err.Message; bNoError = false; } finally { if (con.State.ToString() =="Open") { con.Close(); } } return bNoError; As I stated I have the file that I upload; then if there is an uploaded file, it is "supposed" to read that file into a dataset and write it to the master database. In my web.config file, the master database resides at strConnection2, the uploaded file is at strConnection. I am really confused how to access them both, one to get the data from the uploaded file and two connect to the master database and write to it. Is that better? Please help if you can. Thanks!:confused:

            L²+D

            R Offline
            R Offline
            Rob Graham
            wrote on last edited by
            #5

            First, you need two different connections, one for the input database, the other for the output. If the data in the "upload" copy is just to be appended to the "master" copy (no changes to existing rows in the master), then I would open a data reader on the "upload" table, and use an isert query to insert rows to the master while reading rows from the updload - psuedocode:

            ConnMaster.Open(masterconnstring);
            ConUpload.Open(uploadConnString);
            string sql = "insert into master_table (f1,f2,f3) values (?,?,?)"
            OledbCommand cmmd = new OleDbCommand(ConnMaster,sql);
            using (Datareader dr = ConnUpload.Executereader("select f1,f2,f3from upload table");)
            {
            while (dr.Read())
            {
            try{
            cmd.parameters.clear();
            cmd.parameters.add(new oledbparameter("p1",dr[0].getvalue());
            cmd.parameters.add("p2",dr[1].getvalue());
            cmd.parameters.add("p3",dr[2].getvalue());
            cmd.execute();
            }
            catch(oledbexception e)
            {
            logdata("insert failed" + e.tostring());
            }
            }
            }

            code not exact, please look up correct syntax .

            L 1 Reply Last reply
            0
            • R Rob Graham

              First, you need two different connections, one for the input database, the other for the output. If the data in the "upload" copy is just to be appended to the "master" copy (no changes to existing rows in the master), then I would open a data reader on the "upload" table, and use an isert query to insert rows to the master while reading rows from the updload - psuedocode:

              ConnMaster.Open(masterconnstring);
              ConUpload.Open(uploadConnString);
              string sql = "insert into master_table (f1,f2,f3) values (?,?,?)"
              OledbCommand cmmd = new OleDbCommand(ConnMaster,sql);
              using (Datareader dr = ConnUpload.Executereader("select f1,f2,f3from upload table");)
              {
              while (dr.Read())
              {
              try{
              cmd.parameters.clear();
              cmd.parameters.add(new oledbparameter("p1",dr[0].getvalue());
              cmd.parameters.add("p2",dr[1].getvalue());
              cmd.parameters.add("p3",dr[2].getvalue());
              cmd.execute();
              }
              catch(oledbexception e)
              {
              logdata("insert failed" + e.tostring());
              }
              }
              }

              code not exact, please look up correct syntax .

              L Offline
              L Offline
              L sup2 D
              wrote on last edited by
              #6

              Thanks for your help Rob, but I am not exactly sure where this code would go; after the dset is defined or ??? Also, the insert into (f1,f2,f3) values (?,?,?)" I want to copy all columns, can I put insert into (*)" and leave off values? Sorry to be such a pain, but like I said, I am REALLY new to this and still shakey on my feet (code :o) Thank you again for being such a help to such a bother :-D :rose:

              L²+D

              R 1 Reply Last reply
              0
              • Y Youtea

                You grab your data from strConnection and stored it in the DataSet dsEWEtmp. Now go ahead and open up strConnection2 and insert the data. I'm pretty sure you can find how to insert from dataset very easily from this site.

                Y Offline
                Y Offline
                Youtea
                wrote on last edited by
                #7

                Wow, I can't believe someone gave me 1 point because I didn't put the codes up. Isn't this a C# question anyways?

                L 1 Reply Last reply
                0
                • Y Youtea

                  Wow, I can't believe someone gave me 1 point because I didn't put the codes up. Isn't this a C# question anyways?

                  L Offline
                  L Offline
                  L sup2 D
                  wrote on last edited by
                  #8

                  It probably is a C# question, but as I said, I am new to all this and for me; it has to do with databases as well as the code behind. I don't know who gave you one point, but a little code for pointing in the right direction would have helped. Thanks. :^) :confused:

                  L²+D

                  1 Reply Last reply
                  0
                  • L L sup2 D

                    Thanks for your help Rob, but I am not exactly sure where this code would go; after the dset is defined or ??? Also, the insert into (f1,f2,f3) values (?,?,?)" I want to copy all columns, can I put insert into (*)" and leave off values? Sorry to be such a pain, but like I said, I am REALLY new to this and still shakey on my feet (code :o) Thank you again for being such a help to such a bother :-D :rose:

                    L²+D

                    R Offline
                    R Offline
                    Rob Graham
                    wrote on last edited by
                    #9

                    L wrote:

                    can I put insert into (*)" and leave off values?

                    No, you need to explicitly name the columns (since these are different connections and different databases, * wont work (and is bad practice in any case). Also, the number of columns must match the number of values in the values clause. The order of the parameters must match the field name order. You don't need the dataset at all. It's not helping you. The datareader is a fast replacement for the dataset in this case, it represents the results from the query as a collection of rows that can be read once only in forward order.

                    L 1 Reply Last reply
                    0
                    • R Rob Graham

                      L wrote:

                      can I put insert into (*)" and leave off values?

                      No, you need to explicitly name the columns (since these are different connections and different databases, * wont work (and is bad practice in any case). Also, the number of columns must match the number of values in the values clause. The order of the parameters must match the field name order. You don't need the dataset at all. It's not helping you. The datareader is a fast replacement for the dataset in this case, it represents the results from the query as a collection of rows that can be read once only in forward order.

                      L Offline
                      L Offline
                      L sup2 D
                      wrote on last edited by
                      #10

                      Rob, the following is my code...still having problems. Could you please take a look? :sigh: :^) OleDbConnection con = new OleDbConnection(strConnection); OleDbConnection con2 = new OleDbConnection(strConnection2); con.Open(); con2.Open(); string strSQL = "INSERT INTO First_Table([OID]) values (@OID)"; string strSQL2 = "Select [OID] From First_Table"; OleDbCommand cmd2 = new OleDbCommand(strSQL2,con2); OleDbCommand cmd = new OleDbCommand(strSQL,con); OleDbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { try { cmd.Parameters.Add (new OleDbParameter("OID",dr[0].ToString())); cmd.ExecuteReader(); } catch (Exception err) { string str=err.Message; bNoError=false; } finally { if (con.State.ToString() == "Open") con.Close(); if (con2.State.ToString() == "Open") con2.Close(); }

                      L²+D

                      R 1 Reply Last reply
                      0
                      • L L sup2 D

                        Rob, the following is my code...still having problems. Could you please take a look? :sigh: :^) OleDbConnection con = new OleDbConnection(strConnection); OleDbConnection con2 = new OleDbConnection(strConnection2); con.Open(); con2.Open(); string strSQL = "INSERT INTO First_Table([OID]) values (@OID)"; string strSQL2 = "Select [OID] From First_Table"; OleDbCommand cmd2 = new OleDbCommand(strSQL2,con2); OleDbCommand cmd = new OleDbCommand(strSQL,con); OleDbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { try { cmd.Parameters.Add (new OleDbParameter("OID",dr[0].ToString())); cmd.ExecuteReader(); } catch (Exception err) { string str=err.Message; bNoError=false; } finally { if (con.State.ToString() == "Open") con.Close(); if (con2.State.ToString() == "Open") con2.Close(); }

                        L²+D

                        R Offline
                        R Offline
                        Rob Graham
                        wrote on last edited by
                        #11

                        You need to clear your parameters collection after executing the cmd. Also, don't use executereader here (the insert command), you don't need it and it can only cause problems, use cmd.execute instead. Even if executereader was a typo, continually adding the same parameter (even with a different value) to the parameters collection will cause errors.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups