1

使用实体框架我想将数据添加到文本框。在使用连接字符串和 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;
      }
  }
4

1 回答 1

1

假设您的客户对象如下所示:

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;

    }
于 2013-10-30T13:25:38.920 回答