简体   繁体   English

如何查询包含方括号的列名?

[英]How do I query column names that contain square brackets?

I have a csv that I need to parse into a gridview in vb.net. 我有一个csv,我需要在vb.net中解析为gridview。 If I do SELECT *, I get the data without issue. 如果我做SELECT *,我会毫无问题地得到数据。 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. 问题是,我无法控制生成csv的应用程序,并且它们将列名括在方括号中。

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? 所以我得到了数据的回报?

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. 所以,为了非常清楚:我有一个应用程序,在名为myTable的表中创建一个带有列标题[name]和[height]的csv,对于我的生活,我无法弄清楚如何返回[名称]和[身高]具体​​。

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. 下面的示例在SQL Server中进行了测试。

Script : 脚本

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

Querying all the columns : 查询所有列

SELECT * FROM dbo.myTable

Querying only specific columns : 仅查询特定列

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

VB.NET Code - Sample 1 : 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 Code - Sample 2 : 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)

I'm making the assumption that this is for SQL Server. 我假设这是针对SQL Server的。

Objects that contain the ] character should be quoted using []. 应使用[]引用包含]字符的对象。

For the following table : 对于下表:

CREATE TABLE [t]]] (
  [i]]d] int primary key,
  [da]]]ta] int
)

You can select from it using : 您可以使用以下选项:

SELECT [da]]ta], [i]]d] FROM [t]]]

If you're looking for a way within T-SQL to quote object names, you can use QUOTENAME. 如果您在T-SQL中寻找引用对象名称的方法,则可以使用QUOTENAME。

SELECT QUOTENAME('t]')

Within SQL Server, you can also use double-quotes to quote object names; 在SQL Server中,您还可以使用双引号引用对象名称; however, there are two things you need to validate. 但是,您需要验证两件事。

First you'll need to make sure that you enabled QUOTED_IDENTIFIER. 首先,您需要确保启用了QUOTED_IDENTIFIER。 Some drivers will be this for you automatically. 一些驱动程序将自动为您。

SET QUOTED_IDENTIFIER ON

The second think you'll need to do, is make sure that you escape any other double quotes in your object name. 第二个想到你需要做的是确保你在对象名中转义任何其他双引号。 From T-SQL, QUOTENAME again comes to the rescue. 从T-SQL开始,QUOTENAME再次拯救。

SELECT QUOTENAME('t"]')

"t""]"

In VB.Net you'd want to do something like : 在VB.Net中你想做的事情如下:

dim queryStr As String = String.Format("SELECT ""{1}"" FROM dbo.myTable", String.Replace(col1, """", """"""))

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

相关问题 VBA 查询中列名称内的方括号? - Square brackets inside the name of column in VBA query? 如何在MySQL查询中替换方括号[] - How to replace square brackets [] in mysql query 如何在查询结果中返回列名? - How do I return column names in my query results? 如何为我的 sql 查询的输出数据自动获取列名前缀的表名? - How do I get the table names prefixing the column names automatically for the output data of my sql query? 如果方括号内的内容不包含空格,则删除方括号 - Remove square brackets if content within the square bracket does not contain spaces 如何获取QSqlTableModel的列名? - How do I get the column names of a QSqlTableModel? 我如何创建一个视图,其中从查询返回值定义了列名 - How do I create a view where column names are defined from query return values 如何在SQL中查询表以产生根据条件垂直堆叠列名称的结果? - How do I query a table in SQL to produce a result where column names are stacked vertically based on a condition? 如何对由ID链接但具有不同列名的2个视图运行SQL查询? - How do I run a SQL query against 2 views that are linked by an ID but has different column names? 验证String(query)以查看列名是否包含SQL函数 - Validate String(query) to see if column names contain SQL functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM