in Education by
I had someting like this in my code (.Net 2.0, MS SQL) SqlConnection connection = new SqlConnection(@"Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); connection.Open(); SqlCommand cmdInsert = connection.CreateCommand(); SqlTransaction sqlTran = connection.BeginTransaction(); cmdInsert.Transaction = sqlTran; cmdInsert.CommandText = @"INSERT INTO MyDestinationTable" + "(Year, Month, Day, Hour, ...) " + "VALUES " + "(@Year, @Month, @Day, @Hour, ...) "; cmdInsert.Parameters.Add("@Year", SqlDbType.SmallInt); cmdInsert.Parameters.Add("@Month", SqlDbType.TinyInt); cmdInsert.Parameters.Add("@Day", SqlDbType.TinyInt); // more fields here cmdInsert.Prepare(); Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); char[] delimeter = new char[] {' '}; String[] records; while (!reader.EndOfStream) { records = reader.ReadLine().Split(delimeter, StringSplitOptions.None); cmdInsert.Parameters["@Year"].Value = Int32.Parse(records[0].Substring(0, 4)); cmdInsert.Parameters["@Month"].Value = Int32.Parse(records[0].Substring(5, 2)); cmdInsert.Parameters["@Day"].Value = Int32.Parse(records[0].Substring(8, 2)); // more here complicated stuff here cmdInsert.ExecuteNonQuery() } sqlTran.Commit(); connection.Close(); With cmdInsert.ExecuteNonQuery() commented out this code executes in less than 2 sec. With SQL execution it takes 1m 20 sec. There are around 0.5 milion records. Table is emptied before. SSIS data flow task of similar functionality takes around 20 sec. Bulk Insert was not an option (see below). I did some fancy stuff during this import. My test machine is Core 2 Duo with 2 GB RAM. When looking in Task Manager CPU was not fully untilized. IO seemed also not to be fully utilized. Schema is simple like hell: one table with AutoInt as primary index and less than 10 ints, tiny ints and chars(10). After some answers here I found that it is possible to execute bulk copy from memory! I was refusing to use bulk copy beacuse I thought it has to be done from file... Now I use this and it takes aroud 20 sec (like SSIS task) DataTable dataTable = new DataTable(); dataTable.Columns.Add(new DataColumn("ixMyIndex", System.Type.GetType("System.Int32"))); dataTable.Columns.Add(new DataColumn("Year", System.Type.GetType("System.Int32"))); dataTable.Columns.Add(new DataColumn("Month", System.Type.GetType("System.Int32"))); dataTable.Columns.Add(new DataColumn("Day", System.Type.GetType("System.Int32"))); // ... and more to go DataRow dataRow; object[] objectRow = new object[dataTable.Columns.Count]; Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); char[] delimeter = new char[] { ' ' }; String[] records; int recordCount = 0; while (!reader.EndOfStream) { records = reader.ReadLine().Split(delimeter, StringSplitOptions.None); dataRow = dataTable.NewRow(); objectRow[0] = null; objectRow[1] = Int32.Parse(records[0].Substring(0, 4)); objectRow[2] = Int32.Parse(records[0].Substring(5, 2)); objectRow[3] = Int32.Parse(records[0].Substring(8, 2)); // my fancy stuf goes here dataRow.ItemArray = objectRow; dataTable.Rows.Add(dataRow); recordCount++; } SqlBulkCopy bulkTask = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null); bulkTask.DestinationTableName = "MyDestinationTable"; bulkTask.BatchSize = dataTable.Rows.Count; bulkTask.WriteToServer(dataTable); bulkTask.Close(); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Instead of inserting each record individually, Try using the SqlBulkCopy class to bulk insert all the records at once. Create a DataTable and add all your records to the DataTable, and then use SqlBulkCopy.WriteToServer to bulk insert all the data at once.

Related questions

0 votes
    I had someting like this in my code (.Net 2.0, MS SQL) SqlConnection connection = new SqlConnection ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    टी/The 3. वंचिटित सी ग्टउठ speed of computer is कंप्यूटर की गति .. मज ग O उत/Fast/तेज O यीभी/Slow/ ... THE ABOVE/इनमें से कोई नहीं Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    What is bulk copy or BCP in Oracle?...
asked Dec 17, 2020 in Technology by JackTerrance
0 votes
    I am trying to use change tracking to copy data incrementally from a SQL Server to an Azure SQL Database. I ... change tracking table? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    main memory unit of a computer: a)stores bulk of data and instruction b)stores small amount of data and ... of all other unit Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    I have 7 excel sheets in one workbook and I am trying to copy and paste the data from each excel ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have a very large MySQL table (billions of rows, with dozens of columns) I would like to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    COPY TO exports data from a csv into table (1)False (2)True...
asked Apr 17, 2021 in Technology by JackTerrance
0 votes
    I'm trying to create two dimensional std::vector, that will hold objects of this class I inherited. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I've seen examples that allow you to create a manipulator that inserts delimiters but none of those manipulators are sticky. That is, ... . I want to be able to do this: std::cout...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    How would you deal with slow application complaint from clients?...
asked Jun 11, 2021 in Technology by JackTerrance
0 votes
    I have an array of the shape (100000, 1) with each element in an array of type positive integer and not ... help would be appreciated. Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    convert the following into For loop; while(true) System.out.print(‘*’); (in java ) PLEASE ANSWER FAST… WILL MARK BRAINLIEST** Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Can we make changes on the repo and how we can make sure that the same has been merged into the working copy?...
asked Feb 18, 2021 in Technology by JackTerrance
...