Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Data migration using ADO.NET from a webservice

Posted on 2010-08-19
8
Medium Priority
?
402 Views
Last Modified: 2012-05-10
Hi I would like to migrate data from a web service to oracle database using ADO.NET and I want to it be done in a faster way . Any help will be appreciated. I cannot do bulkcopy since I cannot access the source database, I can just get collection objects which need to be udpated in the database in efficient way (C#)

Please suggest... waiting for your reply.....
0
Comment
Question by:goldmaxhi
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33480209

Something like this:
using (OracleConnection connection = new OracleConnection(connectionString))
            {
                connection.Open();

                foreach(MyObject m in myCollection)
                {
                    // first col is int and other two cols are text type
                    string queryString = @"INSERT INTO Table1 (col1, col2, col3) 
                                           values ({0}, '{1}', '{2}')";
                    queryString = string.Format(queryString,
                                                m.Field1,
                                                m.Field2,
                                                m.Field3);

                    OracleCommand command = new OracleCommand(queryString);
                    command.Connection = connection;                    
                    command.ExecuteNonQuery();
                }
            }

Open in new window

0
 

Author Comment

by:goldmaxhi
ID: 33480273
Hi Kris,

Thanks for the quick response... Can we use threads for this to make it even faster something like producer /consumer model...??
0
 

Author Comment

by:goldmaxhi
ID: 33480283

I am talking about the order for million records....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:kris_per
ID: 33480492

Threads are mainly to provide better user experience/responsive user interface and speed is not guaranteed by using threads (as execution in cpu is switched between different threads/apps by time-slicing)

Thread can be used for the above code...one option is - all of the above can be placed in a thread which will insert row after row...

If you mean the producer /consumer model for each row, though it is possible, I am not sure about the efficiency as it would involve one connection per task. Creating and closing a connection for each task/row wont be an efficient way...again db servers will have max limit for connections (sql server has max 100 connections at a time by default)...If you think about creating one connection and using it in multiple threads..well, i am not sure how that would work...

I will go for the first option of having a thread which will have all of the above code to loop thro collection and insert rows..simple and efficient, i think may be faster than having multiple threads trying to insert...


0
 
LVL 16

Expert Comment

by:kris_per
ID: 33480525

Another improvement that will increase the efficiency is to use query string with parameters, so that it can be pre-compiled using OracleCommand.Prepare method, which I am sure will increase the speed...

0
 

Author Comment

by:goldmaxhi
ID: 33480556
Kris... thank you very much for the answer....so nice of you...
0
 
LVL 16

Accepted Solution

by:
kris_per earned 2000 total points
ID: 33480584

Thanks.....

With command.Prepare method, code will be as shown below...I am sure this will be more efficient than the prev code
using (OracleConnection connection = new OracleConnection(connectionString))
            {
                connection.Open();

                // first col is int and other two cols are text type
                string queryString = @"INSERT INTO Table1 (col1, col2, col3) 
                                           values (@val1, @val2, @val3)";

                OracleCommand command = new OracleCommand(queryString);
                command.Connection = connection;
                command.Parameters.Add("@val1", SqlDbType.Int);
                command.Parameters.Add("@val2", SqlDbType.Text);
                command.Parameters.Add("@val3", SqlDbType.Text);
                command.Prepare();

                foreach(MyObject m in myCollection)
                {                    
                    command.Parameters["@val1"].Value = m.Field1;                    
                    command.Parameters["@val2"].Value = m.Field2;                    
                    command.Parameters["@val3"].Value = m.Field3;

                    command.ExecuteNonQuery();
                }
            }

Open in new window

0
 

Author Closing Comment

by:goldmaxhi
ID: 33599816
Kris was very helpful in answering the question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question