473,396 Members | 1,772 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,396 software developers and data experts.

How to do efficient database insertion from c# 2.0?

Hi,
I'm creating a program that reads data from a number of textfiles,
manipulates the data and then inserts it into a database. There are >200.000
rows and I use a SqlCommand object for each row . The SqlCommand object
calls a stored procedure that I have created.

There are approx. 10-15 different stored procedures that I use.

My problem is that the program is really slow, so what do I do wrong? Is
there a better way to do this?

Thanks,
Mats-Lennart

The code I use look something like this:

SqlConnection con = Utils.GetSqlConnection(); // Reuse the same
connection for all sql commands

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "procname";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("P1", V1));
cmd.Parameters.Add(new SqlParameter("P2", V2));
cmd.Parameters.Add(new SqlParameter("P3", V3));

cmd.Connection = con;

cmd.ExecuteNonQuery();
Aug 10 '06 #1
3 1591
It is rather SQL issue; try to post your question there as well. Anyway, how
long does it take? Taking into account >200.000 operations, say, half an hour
may be quite reasonable time (depending on your database and stored
procedure, of course). Consider also using BULK copy or insert, see SQL
documentation; quotation: “Bulk copy is the fastest way to add large numbers
of rows in SQL Server”.

--
Thanks,
Roman

Aug 10 '06 #2
The first thing to do is to understand what time is being used in the db,
and what in the C#.

I would (literally) comment out all of the ExecuteNonQuery lines, and fake
any return data required (e.g. using a simple counter to simulate IDENTITY
values). It could well be that the time is due to inefficient collection
management or suchlike.
There are approx. 10-15 different stored procedures that I use.
Well, if they are on the same connection, there is no reason that you can't
re-use the existing command objects: just update the parameters before each
call. Could also call .Prepare() after the initial creation, which should
get everything nicely ready...

***However***: I'd be suprised if this made a /massive/ difference - but it
should help; downside is that (depending on the implementation) can make
things less encapsulated.

If the database-server is a long way (ping-wise) from the client, you could
also look at preparing larger batched commands to reduce the round-trip
count. Could also look at the SP / trigger performance; any foobars?

Marc
Aug 10 '06 #3
On Thu, 10 Aug 2006 11:02:06 +0200, "Mats-Lennart Hansson"
<ap********@hotmail.comwrote:
>Hi,
I'm creating a program that reads data from a number of textfiles,
manipulates the data and then inserts it into a database. There are >200.000
rows and I use a SqlCommand object for each row . The SqlCommand object
calls a stored procedure that I have created.

There are approx. 10-15 different stored procedures that I use.

My problem is that the program is really slow, so what do I do wrong? Is
there a better way to do this?

Thanks,
Mats-Lennart

The code I use look something like this:

SqlConnection con = Utils.GetSqlConnection(); // Reuse the same
connection for all sql commands

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "procname";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("P1", V1));
cmd.Parameters.Add(new SqlParameter("P2", V2));
cmd.Parameters.Add(new SqlParameter("P3", V3));

cmd.Connection = con;

cmd.ExecuteNonQuery();
Fundamental problem is that you are doing everything in a row-oriented
fashion, calling the proc once for every row of your data. That's a
lot of to-and-fro, for a start. For large amounts of data you need to
work in terms of sets rather than rows.

Change your program so that you:

1. Load all the data in one hit (look into bulk insert or DTS).

2. Rewrite your proc so that it can process all 200,000 rows in one
go.
This is really more of an SQL question than a C# one.

--
Phil
Aug 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
0
by: code_worthy | last post by:
I am trying to strip some data out of numerous emails and place it in my database. I know that this seems as if it has been done before. But, this is a little different. First, the numerous...
4
by: Tony Williams | last post by:
I have two groups of companies each producing the same statistics, however one group produces them in sterling and the other group (much smaller only 5 companies) produces them in Euros. They...
4
by: Hazzard | last post by:
What is the best way to do this? Binary with 0 representing off and 1 on? Int16 with 1 representing first button, 2 the second, 3 ... varchar with a character values at certain positions in the...
1
by: Tull Clancey | last post by:
Hi, can anyone point me in the right direction for samples or information on creating a good quality Database Connection Class for VB NET? Relatively new to Net and having difficulties getting my...
3
by: Big Charles | last post by:
Hi, We have developed an ASP.NET web application and are planning to host it in an external Server, which provides us a good bandwidht. We need to put he web application outside because the...
10
by: MattC | last post by:
Hi, I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB side paging. However, currently I perform my sorting on the front end. If I perform a sort and then ask for page...
6
by: Randy Shore | last post by:
I have a shared Access mdb that was originally written in A2000. The database is large, around 40 tables, 40 forms (many with multiple sub-forms), and 100s of reports. The database is not split,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.