ASP.NET LINQ SQL 获取特定字段

发布于 12-06 04:55 字数 445 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽2024-12-13 04:55:40

那么您可以选择适当的字段作为开始:

EQCN = Request.QueryString["EQCN"];
var values = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n.FieldYouWant;

// Or possibly Single, or First...
var singleValue = values.FirstOrDefault();

我认为这就是您所追求的,但如果不是,请澄清您的问题。

编辑:要回答您的后续问题,您可以使用:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n;

// Or possibly Single, or First...
var entity = query.Single();

textBox1.Text = entity.Name;
textBox2.Text = entity.Description;
textBox3.Text = entity.Title;
// etc

假设您想要访问实体中的所有内容。如果实体非常大并且您只需要几个字段,您可能想要执行以下操作:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select new { n.Name, n.Description, n.Title };

// Or possibly Single, or First...
var projection = query.Single();

textBox1.Text = projection.Name;
textBox2.Text = projection.Description;
textBox3.Text = projection.Title;

我不确定我是否真的会将数据访问层和 UI 层耦合得如此紧密,但是那是另一回事...

Well you can select the appropriate field to start with:

EQCN = Request.QueryString["EQCN"];
var values = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n.FieldYouWant;

// Or possibly Single, or First...
var singleValue = values.FirstOrDefault();

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:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n;

// Or possibly Single, or First...
var entity = query.Single();

textBox1.Text = entity.Name;
textBox2.Text = entity.Description;
textBox3.Text = entity.Title;
// etc

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:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select new { n.Name, n.Description, n.Title };

// Or possibly Single, or First...
var projection = query.Single();

textBox1.Text = projection.Name;
textBox2.Text = projection.Description;
textBox3.Text = projection.Title;

I'm not sure I'd actually couple the data access and UI layers so closely, but that's a different matter...

清浅ˋ旧时光2024-12-13 04:55:40

您只需执行一次查询,但一旦完成,您就必须将每个字段分配给一个文本框。首先仅检索您想要的单个项目:

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;
    }

You only need to perform the query once, but once that's done, you'll have to assign each field to a TextBox. Start by retrieving only the single item you want:

EQCN = Request.QueryString["EQCN"];
var equipment = (from n in db.equipments
                 where n.EQCN.ToString() == EQCN
                 select n).FirstOrDefault();

Then go through and assign each TextBox to the appropriate field:

txtName.Text = equipment.Name;
txtDescription.Text = equipment.Description;
txtValue1.Text = equipment.Value1;
txtValue2.Text = equipment.Value2;
//...

If you have several dozen TextBoxes to assign, you could set up a custom control that can be databound to an equipment object, but even then, you'll still have to write the binding code for your control.

The only way I can think of to totally automate this process is to name each TextBox after a field in your object, then use reflection to match them to values:

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文