使用 LINQ2SQL 填充文本框

发布于 2024-11-07 07:03:37 字数 743 浏览 0 评论 0原文

在我的 Web 表单中,我有文本框和一个单选列表,我需要通过 LINQ2SQL 查询来填充它们。到目前为止,我编写了这个查询来获取将要填充到数据库中的特定记录。

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        };
        };

现在我知道这个查询要获取的记录将只是 1 个唯一记录。我想在这些文本框中显示记录并选择该单选按钮:

tb名称
tbNum
tbAcctNum
rbtnCorpAcct

我应该怎么做?先感谢您!

In my web forms, I have text boxes and one radiolist which I need to populate through a LINQ2SQL query. So far I coded this query to fetch particular records which is going to be populated into the DB.

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        };
        };

Now I know that the record which this query is going to be fetching is gonna be only 1 unique record. I want to show the record in these textboxes and select that radio button:

tbName
tbNum
tbAcctNum
rbtnCorpAcct

How should I do this? Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

橪书 2024-11-14 07:03:37

非常简单:

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = (from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        }).FirstOrDefault();

             if (query != null)
             {
                tbName.Text = query.Name;
                tbNum.Text = query.Num;
                //and so on
                rbl.SelectedValue = query.SomeValue;
             }
        };

Very simply:

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = (from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        }).FirstOrDefault();

             if (query != null)
             {
                tbName.Text = query.Name;
                tbNum.Text = query.Num;
                //and so on
                rbl.SelectedValue = query.SomeValue;
             }
        };
月光色 2024-11-14 07:03:37

与其他人的回答相同,添加了单选按钮:

tbName.Text = query.Name;
tbNum.Text = query.Num;
tbAcctNum.Text = query.AcctNum;

if(query.CorpAcct)
    rbtn.SelectedValue = "Yes"; \\Where "Yes" is one of the radio button values
else
    rbtn.SelectedValue = "No";

\\Could also use SelectedIndex, rbtn.SelectedIndex = 0 or 1

Same as others have answered with addition of radio button:

tbName.Text = query.Name;
tbNum.Text = query.Num;
tbAcctNum.Text = query.AcctNum;

if(query.CorpAcct)
    rbtn.SelectedValue = "Yes"; \\Where "Yes" is one of the radio button values
else
    rbtn.SelectedValue = "No";

\\Could also use SelectedIndex, rbtn.SelectedIndex = 0 or 1
失眠症患者 2024-11-14 07:03:37

请尝试以下操作:

using (dbTestDataContext db = new dbTestDataContext())
{
    var query = 
    (
        from r in db.Table1
        where r.Code == getCode
        select new
        {
            //Account Info
            r.Name,
            r.Num,
            r.AcctNum,
            r.CorpAcct, //Bool
        }
    ).FirstOrDefault();

    tbName.Text = query.Name;

    ....

};

Try the following:

using (dbTestDataContext db = new dbTestDataContext())
{
    var query = 
    (
        from r in db.Table1
        where r.Code == getCode
        select new
        {
            //Account Info
            r.Name,
            r.Num,
            r.AcctNum,
            r.CorpAcct, //Bool
        }
    ).FirstOrDefault();

    tbName.Text = query.Name;

    ....

};
物价感观 2024-11-14 07:03:37

您需要做的第一件事是从查询中检索单个结果。正如您所写的,您将返回一个 IQueryable 对象,该对象现在存储在变量“query”中

要获取单个对象,请执行以下操作

var myObject = query.SingleOrDefault();

然后您可以访问该对象的各个属性并像这样分配它们

tbName。文本 = myObject.Name

The first thing you need to do is retrieve a single result from your query. As you have it written, you are returning an IQueryable object which is now stored in the variable "query"

To get a single object, do this

var myObject = query.SingleOrDefault();

Then you can access the individual properties of that object and assign them like this

tbName.Text = myObject.Name

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