简体   繁体   English

使用C#和oledb在insert to excel命令中的无效括号?

[英]Invalid bracketing in insert to excel command using c# and oledb?

I am iserting some records to excel file using OLEDB 我正在使用OLEDB将一些记录写入excel文件

    foreach (TblSample rec in LstTblSample)
            {

                OleDbCommand cmdInsert = new OleDbCommand("insert into [sd$]" +
                                                   "([Lab Ref],[D.M.],PH,CP,ADF,NDF,DMD,ME,NIRASH,IFCOW,IFSHEEP,NH3,UFV,UFL,PDIE,PDIN,PDIA,LFU,CFU,SFU) values" +
                                                   "(" + "'" + rec.SampleNo + "'" + "," + "'" + rec.DryMatter + "'" + "," + "'" + rec.pH + "'" + "," +
                                                   "'" + rec.CrudeProtein + "'" + "," + "'" + "" + "'" + "," + "'" + rec.NDF + "'" + "," + "'" + rec.DMD + "'" + "," +
                                                   "'" + rec.ME + "'" + "," + "'" + rec.ASH + "'" + "," + "'" + rec.DMIntakeCattle + "'" + "," +
                                                   "'" + rec.DMIntakeSheep + "'" + "," + "'" + rec.NH3 + "'" + "," + "'" + rec.UFV + "'" + "," +
                                                   "'" + rec.UFL + "'" + "," + "'" + rec.PDIE + "'" + "," + "'" + rec.PDIN + "'" + "," + "'" + rec.PDIA + "'" + "," +
                                                   "'" + rec.LFU + "'" + "," + "'" + rec.CFU + "'" + "," + "'" + rec.SFU + "'" + ")", con);
                cmdInsert.ExecuteNonQuery();
            }

My excel file has a column named DM which is causing this error. 我的excel文件有一个名为DM的列,它导致此错误。 I want to keep this column name in excel because, its used by another software. 我想将此列名称保留在excel中,因为它被另一软件使用。 Is there any work around in C# code for insert to cope with this? 在C#代码中是否有任何解决方法可以解决此问题?

在此处输入图片说明

Where I see a problem is the keyword values in this line 我发现问题所在的是此行中的关键字values

OleDbCommand cmdInsert = new OleDbCommand("insert into [sd$] values" +

In the INSERT statement you usually specify the column fields before calling VALUES . INSERT语句中,通常在调用VALUES之前指定列字段。 Alternatively you can call VALUES without naming the column fields. 或者,您可以调用VALUES而不命名列字段。 In this instance you do both. 在这种情况下,您将同时执行两个操作。 Above VALUES BEFORE assigning the columns causes a problem. 在分配列之前,超出VALUES会导致问题。

One option is to remove the column declaration completely and just do insert into [sd$] values "(" + "'" + rec.SampleNo + - ie is the columns and entries match. You can also try rename the column in the INSERT statement to [DM] and then after the data was inserted rename the column in the excel spreadsheet to [DM] as a routine task using the following code modifying it of course: http://dontbreakthebuild.com/2012/03/16/reading-excel-column-names-in-c-using-excel-interop/ 一种选择是完全删除列声明,仅insert into [sd$] values "(" + "'" + rec.SampleNo + -即列和条目匹配。也可以尝试在INSERT中重命名列语句添加到[DM],然后在插入数据后,使用以下代码对其进行例行修改,将excel电子表格中的列重命名为[DM]作为常规任务: http : //dontbreakthebuild.com/2012/03/16/在Excel中使用Excel互操作读取Excel列名称/

If all else fails make use of the Open XML SDK 2.0 to try and manipulate the document. 如果所有其他方法均失败,请使用Open XML SDK 2.0尝试并操作文档。

I'm out! 我出去了! Good luck. 祝好运。

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

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