如何使用 visual studio 和 oracle 读取带有特殊字符的 excel 列名

how to read excel column names with special characters with visual studio and oracle

朋友们晚安,

我正在将 excel 上传到 oracle,我发现 excel 的某些列在列名称中有特殊字符,这些是:

"FECHA/CONV" AND "N°CODIGO"

如果我删除特殊字符它工作正常,但我不能编辑 excel 文件,我只需要阅读它,

我怎样才能使视觉对象不会在 mappeo 和 select 到 excel sheet.

中产生错误

如有任何帮助,我将不胜感激。

我的确切错误是: 部分设定参数尚未设定值

谢谢,

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim _connString As String = "Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.100.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = ORACLE))); User Id=USER; password=PASS;"
        Dim rutacarpeta As String
        Dim NombreArchivo As String
        Dim tablaBBDD As String
        'Dim ot As OracleTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)
        Using conn As OracleConnection = New OracleConnection(_connString)
            conn.Open()
            Dim ot As OracleTransaction = conn.BeginTransaction(IsolationLevel.ReadCommitted)
            Try

                rutacarpeta = "C:21\"
                NombreArchivo = "FILE_0.xlsx"
                tablaBBDD = "FILE_TEST"
                Dim xlsxConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1;';"
                xlsxConn = String.Format(xlsxConn, rutacarpeta + NombreArchivo).Trim()

                Using excel_con As OleDbConnection = New OleDbConnection(xlsxConn)
                    excel_con.Open()
                    'Dim hoja As String = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing).Rows(0)()
                    Dim command As OleDbCommand = New OleDbCommand("SELECT FECHA/CONV,CODIGO,ACUERDO FROM [FILE$]", excel_con)

                    Dim dr As OleDbDataReader
                    dr = command.ExecuteReader()

                    Using bulkCopy As OracleBulkCopy = New OracleBulkCopy(conn)
                        bulkCopy.BulkCopyTimeout = 90000
                        bulkCopy.DestinationTableName = tablaBBDD
                    bulkCopy.ColumnMappings.Add(New OracleBulkCopyColumnMapping("FECHA/CONV", "FECHA"))
                    bulkCopy.ColumnMappings.Add(New OracleBulkCopyColumnMapping("N°CODIGO", "CODIGO"))
                    bulkCopy.ColumnMappings.Add(New OracleBulkCopyColumnMapping("ACUERDO", "ACUERDO"))
                        bulkCopy.WriteToServer(dr)
                        ot.Commit()

                        bulkCopy.Close()
                    End Using
                End Using

            Catch ex As Exception
                Try
                    ot.Rollback()
                Catch ex1 As Exception
                    MessageBox.Show(ex1.Message)
                End Try

                MessageBox.Show(ex.Message)

            End Try
        End Using
    End Sub

当我指出您引用了 table 名称时,您可能会踢自己,因为它包含一个特殊字符,并且有效..您只是忘记引用列名称

SELECT [FECHA/CONV],[CODIGO],[ACUERDO] FROM [FILE$]

他们并不都需要引用,但这不会有什么坏处。此外,如果您在设置批量复制列映射时在 Oracle 端遇到任何错误,可以考虑将 excel 名称别名化为更简单的名称,并使用

SELECT [FECHA/CONV] as A,CODIGO,ACUERDO FROM [FILE$]

...

...Add(New OracleBulkCopyColumnMapping("A", "FECHA"))