我有一个csv,我需要在vb.net中解析为gridview.如果我做SELECT *,我会毫无问题地得到数据.但是,我需要引用实际的列名称.问题是,我无法控制生成csv的应用程序,并且它们将列名括在方括号中.
我怎么能这样做:
Dim cmdSelect As New OleDbCommand(SELECT "[name],[height] FROM myTable")
所以我得到了数据的回报?
所以,为了非常明确:我有一个应用程序,在名为myTable的表中创建一个带有列标题[name]和[height]的csv,对于我的生活,我无法弄清楚如何返回[name]和[height]具体.
解决方法
如果列名称具有方括号,则可以使用双引号来包围列名称.下面的示例在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)