datagridview中的SQL查询列名

时间:2010-08-16 08:37:21

标签: sql datagridview

我有两个表test1 { id, name }test2 { id, name, family },我写了这个查询:

SELECT dbo.test1.*, dbo.test2.*
FROM dbo.test1
CROSS JOIN dbo.test2

但在datagridview1中,我想显示字段(在标题上),如下所示:

test1.id test1.name test2.id test2.name test2.family

但是它们会像这样显示

id name id name family

我的查询需要进行哪些更改。

5 个答案:

答案 0 :(得分:3)

简短的回答是:您可以根据需要更改网格列的标题。您也可以根据需要重新排序/隐藏/排序列。

答案 1 :(得分:3)

我会写选择:

select t1.id as "test1.Id", 
       t1.name as "test1.Name", 
       t2.id as "test2.Id", 
       t2.name as "test2.Name", 
       t2.family as "Test2.Family" 
from test1 t1, test2 t2

但是如果您没有添加适当的Where子句,那么使用该查询您将获得笛卡尔积。

答案 2 :(得分:1)

您需要单独选择列,并使用as关键字:

SELECT dbo.test1.id as test1id, dbo.test2.id as test2id ...
FROM dbo.test1
CROSS JOIN dbo.test2

答案 3 :(得分:1)

您是否在询问如何为列添加别名?

SELECT
    t1.id AS [test1.id],
    t1.name AS [test1.name] ,
    t2.id AS [test2.id], 
    t2.name AS [test2.name] ,
    t2.family AS [test2.family]
FROM dbo.test1 t1
CROSS JOIN dbo.test2 t2

由于您需要的名称不符合标识符的标准规则,因此需要引用它们。

答案 4 :(得分:1)

糟糕的代码很糟糕。但是,我会像@Scoregraphic提到的那样做。即使您的所有列都返回co1; col1; col1,您也可以更改订单和标签。使用DataGridView中列的属性。