开发者

SQL Query works in management studio but not in C#

I have a problem doing a query with my software...

I have a function which builds my query, and then execute it to fill a gridview. But it keep throwing me an exception : "The multi-part identifier "" could not be bound" with several columns.

When I try the query in SQL Management Studio, it just works fine. I really don't understand. Here's the query.

SELECT DISTINCT [NonConformite].[Numero],
CAST ([NonConformite].[Numero] AS varchar (255)) AS Champ1, 
CAST ([NonConformiteDonneesComplementaires177_4].[Texte2] AS varchar (255)) AS Champ2, 
CAST (NonConformite.dbo.[NonConformiteStatut].[Libelle] AS varchar (255)) AS Champ3, 
CAST ([NonConformite].[Description] AS varchar (255)) AS Champ4 FROM [NonConformite].[dbo].[NonConformite]  
LEFT JOIN NonConformite.dbo.[NonConformiteDonneesComplementaires] as NonConformiteDonneesComplementaires177_4 
ON [NonConformite].[Numero] = [NonConformiteDonneesComplementaires177_4].[Numero] 
LEFT JOIN NonConformite.dbo.[NonConformiteStatut] 
ON [NonConformite].[CpteurStatut] = NonConformite.dbo.[NonConformiteStatut].[Cpteur]

I work on Windows 7 Pro, with Visual Studio 2008, SQL Server 2008 and in ASP.NET C#.

Thank you for your开发者_如何学Python answers !


I would start by breaking the query into aliases - see if that helps things; and unless you are intentionally doing cross-db work (usually a bad idea), drop the db identifier and just use the current db:

SELECT DISTINCT nc.[Numero],
CAST (nc.[Numero] AS varchar (255)) AS Champ1, 
CAST (dc.[Texte2] AS varchar (255)) AS Champ2, 
CAST (st.[Libelle] AS varchar (255)) AS Champ3, 
CAST (nc.[Description] AS varchar (255)) AS Champ4
FROM dbo.[NonConformite] nc
LEFT JOIN dbo.[NonConformiteDonneesComplementaires] dc
ON nc.[Numero] = dc.[Numero] 
LEFT JOIN dbo.[NonConformiteStatut] st
ON nc.[CpteurStatut] = st.[Cpteur]


Try running the following, and remove the comments from the bottom upwards till you can identify where the problem is.

SELECT *
--DISTINCT [NonConformite].[Numero],
--CAST ([NonConformite].[Numero] AS varchar (255)) AS Champ1, 
--CAST ([NonConformiteDonneesComplementaires177_4].[Texte2] AS varchar (255)) AS Champ2, 
--CAST (NonConformite.dbo.[NonConformiteStatut].[Libelle] AS varchar (255)) AS Champ3, 
--CAST ([NonConformite].[Description] AS varchar (255)) AS Champ4
FROM [NonConformite].[dbo].[NonConformite]  
-- LEFT JOIN NonConformite.dbo.[NonConformiteDonneesComplementaires] as NonConformiteDonneesComplementaires177_4 
    ON [NonConformite].[Numero] = [NonConformiteDonneesComplementaires177_4].[Numero] 
-- LEFT JOIN NonConformite.dbo.[NonConformiteStatut] 
    ON [NonConformite].[CpteurStatut] = NonConformite.dbo.[NonConformiteStatut].[Cpteur]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜