你當然可以。您可以直接發出SQL調用,如果你喜歡,如:
public partial class MainWindow : Window
{
public ObservableCollection<Customer> Customers { get; set; }
public MainWindow()
{
InitializeComponent();
Customers = new ObservableCollection<Customer>();
var conString = "MyConnectionString";
using (var con = new SqlConnection(conString))
{
con.Open();
var sql = "Select Name from Customer";
var cmd = new SqlCommand(sql, con);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var c = new Customer
{
Name = reader[0].ToString()
};
Customers.Add(c);
}
}
}
}
}
public class Customer
{
public string Name { get; set; }
}
然後您可以將DataGrid綁定到客戶收集的XAML。
忘記投票作爲答案。謝謝@凱利,它的工作! – 2014-10-02 13:51:21