如何使用Linq To Sql从数据库调用数据到Texbox [英] How Will I Call Data To Texboxes From Database Using Linq To Sql
本文介绍了如何使用Linq To Sql从数据库调用数据到Texbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个,没有错误但它不起作用
i have this, no error but it doesn't work
POSDataContext pos = new POSDataContext();
Credential c = new Credential();
var q = from m in pos.Credentials where c.UserName == usernamelabel.Text select m;
fnamelabel.Text = c.FirstName;
lnamelabel.Text = c.LastName;
pnumberlabel.Text = c.PhoneNumber;
dateofcreationlabel.Text = c.DateCreated.ToString();
privilegelabel.Text = c.Privilege;
推荐答案
查看你的Linq查询:
Take a look at your Linq query:
var q = from m in pos.Credentials where c.UserName == usernamelabel.Text select m;
c
在那里做什么?
我想 q
会返回 IEnumerable< Credential>
。因此,您必须遍历Credential集合才能使用其属性:
What c
is doing there?
I guess that q
returns a IEnumerable<Credential>
. So, you have to loop through the collection of Credential to be able to use its properties:
foreach(Credential c in q)
{
Console.WriteLine("{0}", c.FirstName);
}
但是......如果你想要退回单个物品,试试这个:
But... if you would like to return single object, try this:
var q = pos.Credentials.Where(c=>c.UserName == "User1").SingleOrDefault();
然后:
then:
fnamelabel.Text = q.FirstName
这篇关于如何使用Linq To Sql从数据库调用数据到Texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文