sqlbulkcopy

Fastest way to insert 30 thousand rows in a temp table on SQL Server with C#

喜你入骨 提交于 2019-11-30 17:40:22
I am trying to find out how I can improve my insert performance in a temporary table in SQL Server using c#. Some people are saying that I should use SQLBulkCopy however I must be doing something wrong as it seems to work much slower than simply building an SQL insert string instead. My code to create table using SQLBulkCopy is below: public void MakeTable(string tableName, List<string> ids, SqlConnection connection) { SqlCommand cmd = new SqlCommand("CREATE TABLE ##" + tableName + " (ID int)", connection); cmd.ExecuteNonQuery(); DataTable localTempTable = new DataTable(tableName); DataColumn

How does SqlBulkCopy Work

你。 提交于 2019-11-30 16:52:49
问题 I am familiar with the C# SqlBulkCopy class where you can call the 'WriteToServer' method passing through a DataTable. My question is what underlying mechanism in SQL server is used to bulk insert that data? The reason I ask is that the bulk insert referenced in the Bulk Insert MSDN T-SQL help file requires a data file to import. Does the SqlBulkCopy create a data file? I would like to understand this stuff to work out whether I can use the bulk insert functionality in SQL. If I write a SQL

SQLBulkCopy or Bulk Insert

≡放荡痞女 提交于 2019-11-30 11:42:29
I have about 6500 files for a sum of about 17 GB of data, and this is the first time that I've had to move what I would call a large amount of data. The data is on a network drive, but the individual files are relatively small (max 7 MB). I'm writing a program in C#, and I was wondering if I would notice a significant difference in performance if I used BULK INSERT instead of SQLBulkCopy. The table on the server also has an extra column, so if I use BULK INSERT I'll have to use a format file and then run an UPDATE for each row. I'm new to forums, so if there was a better way to ask this

Fastest way to insert 30 thousand rows in a temp table on SQL Server with C#

早过忘川 提交于 2019-11-30 01:12:08
问题 I am trying to find out how I can improve my insert performance in a temporary table in SQL Server using c#. Some people are saying that I should use SQLBulkCopy however I must be doing something wrong as it seems to work much slower than simply building an SQL insert string instead. My code to create table using SQLBulkCopy is below: public void MakeTable(string tableName, List<string> ids, SqlConnection connection) { SqlCommand cmd = new SqlCommand("CREATE TABLE ##" + tableName + " (ID int)

SQL Bulk insert with parent/child relationships, is order preserved?

被刻印的时光 ゝ 提交于 2019-11-29 22:53:15
问题 Similar to these other questions noted below, I have two tables with the structure: create table parent ( recno int identity(1,1) primary key not null, groupCode int, parentdata varchar(80) ); create table child ( parentrecno int not null, childdata varchar(80) ) I need to insert a few hundred thousand records quickly into these tables -- and the tables hold millions of other records unrelated to this insert and are never quiet. Because of the parent/child nature, it's not a good candidate

SQLBulkCopy or Bulk Insert

南楼画角 提交于 2019-11-29 17:30:15
问题 I have about 6500 files for a sum of about 17 GB of data, and this is the first time that I've had to move what I would call a large amount of data. The data is on a network drive, but the individual files are relatively small (max 7 MB). I'm writing a program in C#, and I was wondering if I would notice a significant difference in performance if I used BULK INSERT instead of SQLBulkCopy. The table on the server also has an extra column, so if I use BULK INSERT I'll have to use a format file

SQLBulkCopy inserts a new row with NULL values for all columns

点点圈 提交于 2019-11-29 16:59:57
I have this code which works fine and loads the excel data into an SQL table. The only problem is that it also inserts a new row with NULL values for all columns. using (OleDbConnection excel_con = new OleDbConnection(conString)) { excel_con.Open(); string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString(); DataTable dtExcelData = new DataTable(); using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + Path.GetFileName(excelPath) + "]", excel_con)) { oda.Fill(dtExcelData); } excel_con.Close(); using (SqlConnection con = new

How to add gridview rows to a datatable?

℡╲_俬逩灬. 提交于 2019-11-29 11:28:01
I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation... I have found this http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx But i want all columns of my gridview to be added to the datarow of the datatable Grid http://img85.imageshack.us/img85/4044/gridp.jpg I want to convert gridview to datatable on submit.... Any suggestion... EDIT: Answer below works and i have found an answer too... DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)

SqlBulkCopy into table that Default column values fails when source DataTable row has DBNull.Value

假如想象 提交于 2019-11-29 07:21:54
Update: Here is my solution I have a table defined as: CREATE TABLE [dbo].[csvrf_References] ( [Ident] [int] IDENTITY(1,1) NOT NULL, [ReferenceID] [uniqueidentifier] NOT NULL DEFAULT (newsequentialid()), [Type] [nvarchar](255) NOT NULL, [Location] [nvarchar](1000) NULL, [Description] [nvarchar](2000) NULL, [CreatedOn] [datetime] NOT NULL DEFAULT (getdate()), [LastUpdatedOn] [datetime] NOT NULL DEFAULT (getdate()), [LastUpdatedUser] [nvarchar](100) NOT NULL DEFAULT (suser_sname()), CONSTRAINT [PK_References] PRIMARY KEY NONCLUSTERED ([ReferenceID] ASC) ) ON [PRIMARY] I have a DataTable with

How to use SqlBulkCopyColumnMappingCollection?

青春壹個敷衍的年華 提交于 2019-11-29 03:45:47
I want to make one SqlBulkCopy method that I can use for all my bulk inserts by passing in specific data through the parameters. Now I need to do mapping on some of them. I don't know how to make a SqlBulkCopyColumnMappingCollection since that was my plan to pass in the mapping collection in and use it. However I don't know how to make it. I can't make a new object of it. This is what I have now. How can I add it do mapping put pass it in? public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize) { // Get the DataTable DataTable dtInsertRows = dataTable; using