2016-07-19 49 views
0

我有以下查詢其工作在SQL Server 2008罰款,但是當我在ADO.NET連接,它顯示了這個錯誤:如何ado.net解決這個錯誤「的多部分標識符」

The multi-part identifier "v.ClientId" could not be bound.
The multi-part identifier "v.ClientId" could not be bound.
The multi-part identifier "v.ClientID" could not be bound.
The multi-part identifier "v.Fare" could not be bound.
The multi-part identifier "v.ClientId" could not be bound.

查詢:

SELECT 
    f.ID, f.ClientID, vc.Name, 
    f.Fare as FixFares, v.Fare as VehicleFare, 
    f.FromPostCode, f.ToPostCode, f.[From], f.[To], 
    v.ClientId, v.IsActive, v.VehicleID, vc.ID 
FROM 
    VehicleFixeFare AS v 
INNER JOIN 
    FixFare f ON v.ClientId = f.ClientId 
INNER JOIN 
    Vehicle vc ON v.ClientId = vc.ClientId 
WHERE 
    v.ClientID = 159 

代碼如下

conn = new SqlConnection(Connection1); 
conn.Open(); 

// var LIST=(from a in General.GetQueryable<Ve>) 
string Query = "select f.ID, f.ClientID, vc.Name, f.Fare as FixFares, v.Fare as VehicleFare, f.FromPostCode, "+ 
       "f.ToPostCode, f.[From], f.[To], v.ClientId, v.IsActive, v.VehicleID, vc.ID " + 
       "from VehicleFixeFare as v"+ 
       "inner join FixFare f on v.ClientId = f.ClientId inner join Vehicle vc on v.ClientId = vc.ClientId where v.ClientID =" + ClientID; 

SqlDataAdapter ad = new SqlDataAdapter(Query, conn); 
dt = new DataTable(); 

ad.Fill(dt); 
+7

如果你看看你的字符串,你應該注意到'VehicleFixeFare的別名與v''內連接之間沒有空格。此外,我還建議更改查詢以防止潛在的SQL注入攻擊,如果clientID是freetype或可以由用戶以某種方式操縱它可能有潛在危險。期待將其更改爲參數。 –

+0

創建一個存儲過程。至少,ClientID應該是一個參數。 –

回答

0

這是由於空間問題。在每一行的開始和結尾留出一些空間,您的問題將得到解決。

string Query = " select f.ID, f.ClientID, vc.Name, f.Fare as FixFares, v.Fare  as VehicleFare, f.FromPostCode, "+ 
      " f.ToPostCode, f.[From], f.[To], v.ClientId, v.IsActive, v.VehicleID, vc.ID " + 
      " from VehicleFixeFare as v "+ 
      " inner join FixFare f on v.ClientId = f.ClientId inner join Vehicle vc on v.ClientId = vc.ClientId where v.ClientID =" + ClientID; 
0

我會做這樣的代碼,這將消除空間的問題很可能錯誤的原因,並且更具可讀性。

string Query = 
     @"select f.ID, f.ClientID, vc.Name, f.Fare as FixFares, v.Fare as VehicleFare, f.FromPostCode, 
        f.ToPostCode, f.[From], f.[To], v.ClientId, v.IsActive, v.VehicleID, vc.ID 
      from VehicleFixeFare as v 
      inner join FixFare f on v.ClientId = f.ClientId 
      inner join Vehicle vc on v.ClientId = vc.ClientId where v.ClientID =" + ClientID; 
相關問題