ASP.NET LINQ SQL 获取特定字段
我正在尝试使用 LINQ - SQL 在 ASP.NET 网页上设置文本框。这是我必须执行 select 语句的代码:
EQCN = Request.QueryString["EQCN"];
var equipment = from n in db.equipments
where n.EQCN.ToString() == EQCN
select n;
How do I set TextBox1.text to be a certain field in the table?
非常感谢
编辑
我需要将表中的每个字段输出到不同的文本框中。因此,对单个查询执行一次查询似乎有点多。一定有办法做到这一点吗?
谢谢
I am trying o set textboxes on an ASP.NET webpage using LINQ - SQL. Here is the code I have to perform the select statement:
EQCN = Request.QueryString["EQCN"];
var equipment = from n in db.equipments
where n.EQCN.ToString() == EQCN
select n;
How do I set TextBox1.text to be a specific field in the table?
Thanks so much
EDIT
I need to output every field in the table into different textboxes. So performing a query for ever single one seems a little much. There has to be a way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
您只需执行一次查询,但一旦完成,您就必须将每个字段分配给一个文本框。首先仅检索您想要的单个项目:
EQCN = Request.QueryString["EQCN"];
var equipment = (from n in db.equipments
where n.EQCN.ToString() == EQCN
select n).FirstOrDefault();
然后遍历并将每个文本框分配给适当的字段:
txtName.Text = equipment.Name;
txtDescription.Text = equipment.Description;
txtValue1.Text = equipment.Value1;
txtValue2.Text = equipment.Value2;
//...
如果您有几十个文本框要分配,您可以设置一个可以将数据绑定到设备的自定义控件。 code> 对象,但即便如此,您仍然需要为控件编写绑定代码。
我能想到的完全自动化此过程的唯一方法是在对象中的字段之后命名每个 TextBox,然后使用反射将它们与值匹配:
var textboxes = Panel1.Controls.OfType<TextBox>();
foreach (TextBox txt in textboxes)
{
string fieldname = txt.ID.Remove(0, 3); //"txtDescription" becomes "Description"
string value = equipment.GetType().GetProperty(fieldname).GetValue(equipment, null) as string;
txt.Text = value;
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
那么您可以选择适当的字段作为开始:
我认为这就是您所追求的,但如果不是,请澄清您的问题。
编辑:要回答您的后续问题,您可以使用:
假设您想要访问实体中的所有内容。如果实体非常大并且您只需要几个字段,您可能想要执行以下操作:
我不确定我是否真的会将数据访问层和 UI 层耦合得如此紧密,但是那是另一回事...
Well you can select the appropriate field to start with:
I think that's what you were after, but if it's not, please clarify your question.
EDIT: To answer your follow-up, you can use:
That's assuming you want to have access to everything in the entity. If the entity is very large and you only need a few fields, you might want to do something like this:
I'm not sure I'd actually couple the data access and UI layers so closely, but that's a different matter...