简体   繁体   English

用于Excel电子表格创建的OLEDB和C#批量插入

[英]OLEDB and C# bulk insert for Excel spreadsheet creation

I am using C# to read a SQL stored procedure, put the results of the stored procedure into a C# data table and then reading the data table row by row to build up my "Insert into....values "etc. 我正在使用C#读取SQL存储过程,将存储过程的结果放入C#数据表中,然后逐行读取数据表以建立“插入到...值”等。 This creates my Excel spreadsheet with the correct data. 这将创建具有正确数据的Excel电子表格。 However, instead of inserting row by row, is there a way of doing a bulk insert? 但是,除了逐行插入外,还有其他方法可以进行批量插入吗?

Failing that I was thinking of getting the stored procedure to write the results to a permanent table and therefore is there a way of doing an "Insert into ....select from ". 如果我没有想到要让存储过程将结果写到永久表中,那么就有办法进行“插入.... select from”。 When I have tried this in C# the code is unable to find the SQL table name specified "Microsoft database access engine cannot find the object ", what is the correct syntax and where do you specify where/how to access the SQL table? 当我在C#中尝试过此代码时,代码无法找到指定的“ Microsoft数据库访问引擎无法找到对象”指定的SQL表名称,正确的语法是什么?在哪里指定在何处/如何访问SQL表?

Thanks 谢谢


Hi, that link looks like it's using Microsoft.Office.Interop.Excel; 嗨,该链接看起来像是在使用Microsoft.Office.Interop.Excel; I'm using OLEDB (which i'm now beginning to regret!). 我正在使用OLEDB(现在我开始感到后悔!)。 So basically i have a C# class that is called from another component. 所以基本上我有一个从另一个组件调用的C#类。 This C# class reads a sql stored procedure, puts the results into a data table. 此类C#类读取sql存储过程,并将结果放入数据表中。 I then set up the table definition using the OLEDBcommand "Insert into ( []) values ("?"). I then define the parameters eg cmd.Parameters.Add(columnHeading, OleDbType.VarChar, size) etc. Then for each row i find in the data table i set the cmd.Parameters[i].value = row[i], where parameters[i] is incremented for each column in that row. I then loop round for each data row and set cmd.Parameters[i].value appropriately. As I have to set the cmd.Parameters[i].Value for each row i find in my dataset and then cmd.ExecuteNonQuery(); , this is quite time consuming. So is there a way to bulk insert the data from the data table into the OLEDB command, if not, can i insert the data by referencing a SQL table directly and doing a "insert into..select from"? 然后,我使用OLEDB命令“插入([])值(“?”)来设置表定义。然后定义参数,例如cmd.Parameters.Add(columnHeading,OleDbType.VarChar,size)等。然后针对每一行我在数据表中找到了我设置的cmd.Parameters [i] .value = row [i],其中该行中每一列的parameter [i]都递增,然后为每个数据行循环并设置cmd.Parameters [i] .value适当,因为我必须为我在数据集中找到的每一行设置cmd.Parameters [i] .Value,然后设置cmd.ExecuteNonQuery();,这非常耗时,因此有一种方法可以将数据表中的数据批量插入到OLEDB命令中,如果没有,我是否可以通过直接引用SQL表并执行“插入..select from”来插入数据?

You can separate your insert statements with a semicolon and run just 1 command. 您可以用分号分隔插入语句,仅运行1条命令。 For instance ... 例如 ...

string sql = "insert into table (col1,col2) values ('row1','row1');"
sql += "insert into table (col1,col2) values ('row2','row2');"
sql += "insert into table (col1,col2) values ('row3','row3');"
SqlConnection conn = new SqlConnection("some connection string");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
conn.ExecuteNonQuery();
conn.Dispose();
cmd.Dispose();

Or something similar. 或类似的东西。 You are executing all 3 queries with 1 command. 您正在使用1条命令执行所有3个查询。 This might not work with databases other than SQL Server, but will definitely work with SQL Server. 这可能不适用于SQL Server以外的数据库,但肯定可以用于SQL Server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM