2014-03-01 46 views
1

什麼會顯示這條SQL語句的結果的最佳方式:把從SQL查詢信息到一個DataGridView

select "WorkOrder"."WorkOrderID", "Customer"."FirstName", "Vehicles"."Model", "WorkOrder"."State" 
from "WorkOrder", "Customer", "Vehicles" 
WHERE "WorkOrder"."VIN" = "Vehicles"."VIN" 
    AND "Vehicles"."CustomerID" = "Customer"."CustomerID" 
    AND "WorkOrder"."State" = 'In Progress'; 

我使用C#的WinForms與Visual Studio 2010中我確實不能似乎弄清楚如何把它放在DataGridView或類似的東西。有沒有辦法做或將其作弊到DataGridView?

+0

您可以在'DataTable'中使用它,並將其分配給'DataGridView.DataSource'屬性。不記得assing'AutoGenerateColumns'到TRUE;。看看的例子,從文檔http://msdn.microsoft.com/tr-tr/library/system.windows.forms.datagridview.datasource(v=vs.110).aspx –

+0

我怎麼會去製作一個那些? – user3169698

回答

0

最簡單的方法將被放在一個DataTable並assing到.DataSource財產等;

string command = @"select "WorkOrder"."WorkOrderID", "Customer"."FirstName", "Vehicles"."Model", "WorkOrder"."State" from "WorkOrder", "Customer", "Vehicles" WHERE "WorkOrder"."VIN" = "Vehicles"."VIN" AND "Vehicles"."CustomerID" = "Customer"."CustomerID" AND "WorkOrder"."State" = 'In Progress'"; 

using(SqlConnection conn = new SqlConnection(connectionString)) 
using(SqlCommand command = conn.CreateCommand()) 
{ 
    command.Connection = conn; 
    using(SqlDataAdapter adapter = new SqlDataAdapter()) 
    { 
     adapter.SelectCommand = command; 
     DataTable table = new DataTable(); 
     adapter.Fill(table); 
     DataGridview1.DataSource = table; 
     DataGridview1.AutoGenerateColumns = true; 
    } 
} 
+0

可悲的是,這並不在我的環境中工作。 @符號不想工作。 – user3169698

+0

此外,字符串命令似乎與SqlCommand的命令干擾。 – user3169698