OleDbDataAdapter字段名称包含空格时的语法错误 [英] OleDbDataAdapter Syntax error when field names have spaces

查看:101
本文介绍了OleDbDataAdapter字段名称包含空格时的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从MS Access表中读取数据:

Dim da As OleDb.OleDbDataAdapter,str_sql As String,con As New OleDb.OleDbConnection,str_File as String
Dim str_Table as String
con.ConnectionString =" PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source =" &安培; str_File
con.Open()
str_sql =" SELECT * FROM" &安培; str_Table
da =新OleDb.OleDbDataAdapter(str_sql,con)
da.Fill(ds_WholeTable," Agencies")
con.Close()

其中 str_File 是我正在读取的数据库的完整路径和 str_Table 是数据库中表的名称。

我在查询表达式'Loan No'中得到'语法错误(缺少运算符)。 '最后一行中的错误,因为字段名称中有空格。如果我将SQL更改为

SELECT [Loan No] AS LoanNo等等,那么它工作正常。但是,我的应用程序需要使用任何连接到它的表,我对字段名称没有任何控制,有多少等等。所以我必须使用SELECT *,因此不能将字段名称括在方括号中。< br>
如何阻止发生此错误?

解决方案

在哪一行代码是否发生错误?使用"SELECT *"时无法重现。




I have the following code to read the data from an MS Access table:

Dim da As OleDb.OleDbDataAdapter, str_sql As String, con As New OleDb.OleDbConnection, str_File as String
Dim str_Table as String
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & str_File
con.Open()
str_sql = "SELECT * FROM " & str_Table
da = New OleDb.OleDbDataAdapter(str_sql, con)
da.Fill(ds_WholeTable, "Agencies")
con.Close()

where str_File is the full path to the database I am reading from and str_Table is the name of the table in the database.

I get 'Syntax error (missing operator) in query expression 'Loan No'.' error in the last line because the field name has a space in it. If I change the SQL to

SELECT [Loan No] AS LoanNo, etc...

then it works fine. However, my application needs to work with any table connected to it where I do not have any control over the field names, how many there are etc. so I have to use SELECT * and thus cannot enclose the field names in square brackets.

How can I stop this error from occurring?

解决方案

On what line of code does the error occur? I can't reproduce it when using "SELECT *".

 

 


这篇关于OleDbDataAdapter字段名称包含空格时的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆