[英]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.