使用Entity框架我想将数据添加到文本框中。我在使用连接字符串和myReader之前已经这样做了。但是EF新手。
private void displayCust()
{
using (Entities c = new Entities())
{
cbUsers.ItemsSource = c.customer.ToList();
}
}
显示:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (Entities c = new Entities())
{
string sFirst = c.customer.ToString();
txtFirst.Text = sFirst;
}
}
答案 0 :(得分:0)
假设您的客户对象如下所示:
public class Customer
{
public int ID { get; set; }
public string Name {get;set}
}
您可以像这样获取数据库中第一行的名称:
using (Entities c = new Entities())
{
string sFirst = c.customer.FirstOrDefault().Name.ToString();
txtFirst.Text = sFirst;
}
或具有特定ID的客户名称:
using (Entities c = new Entities())
{
string sFirst = c.customer.FirstOrDefault(x => x.ID == 2).Name.ToString();
txtFirst.Text = sFirst;
}