如何查询包含方括号的列名? [英] How do I query column names that contain square brackets?

查看:31
本文介绍了如何查询包含方括号的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在 vb.net 中解析为 gridview 的 csv.如果我做 SELECT *,我可以毫无问题地获得数据.但是,我处于需要引用实际列名的情况.问题是,我无法控制生成 csv 的应用程序,它们将列名括在方括号中.

I have a csv that I need to parse into a gridview in vb.net. If I do SELECT *, I get the data without issue. However, I am in a situation where I need to reference the actual column names. And problem is, I have no control over the app that generates the csv, and they make the column names enclosed in square brackets.

我怎么能做这样的事情:

How the heck can I do something like this:

Dim cmdSelect As New OleDbCommand(SELECT "[name], [height] FROM myTable")

所以我得到了数据的回报?

so I get a return of data?

所以,为了清楚起见:我有一个应用程序可以在名为 myTable 的表中创建一个带有列标题 [name] 和 [height] 的 csv,而我一生都无法弄清楚如何具体返回[name]和[height].

So, in an effort to be perfectly clear: I have an app that creates a csv with the column headers [name] and [height] in a table called myTable, and for the life of me I can't figure out how to return [name] and [height] specifically.

推荐答案

如果列名有方括号,那么您可以使用双引号将列名括起来.以下示例在 SQL Server 中进行了测试.

If the column names have square brackets, then you can use double quotes to wrap around the column names. Below sample was tested in SQL Server.

脚本:

CREATE TABLE dbo.myTable
(
        "[name]" varchar(max) not null
    ,   "[height]" int not null
);

查询所有列:

SELECT * FROM dbo.myTable

仅查询特定列:

SELECT "[name]", "[height]" FROM dbo.myTable

VB.NET 代码 - 示例 1:

Dim query As String = String.Format("SELECT {0}{1}{0}, {0}{2}{0} FROM dbo.myTable", """", "[name]", "[height]")
Dim cmdSelect As New OleDbCommand(query)

VB.NET 代码 - 示例 2:

Dim query As String = String.Format("SELECT {0}[name]{0}, {0}[height]{0} FROM dbo.myTable", """")
Dim cmdSelect As New OleDbCommand(query)

这篇关于如何查询包含方括号的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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