简体   繁体   English

C#SQL查询结果到文本框

[英]C# SQL query result to textboxes

Newbie here, working on converting a little program I had in python/sqlite to C#/SQL Server. 新手在这里,致力于将我在python / sqlite中拥有的一个小程序转换为C#/ SQL Server。 I'm having a bit of an issue with getting a SQL query to write its results to two textboxes (textBox1 and textBox2) based upon the selection of a combobox (which does populate correctly, by the way). 我有一个问题,要根据选择的组合框(顺便说一句正确填充),使SQL查询将其结果写入两个文本框(textBox1和textBox2)。 Here's my attempted code, just need the last bit that writes the result to the textboxes: 这是我尝试的代码,只需要将结果写到文本框的最后一点即可:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Clear();
        string selected = (this.comboBox1.SelectedIndex).ToString();
        var selected2 = from branches in branchesDataSet.branches
                        where branches.branchCode == "selected"
                        select new
                        {
                            branches.branchCode
                            branches.branchName
                        };

I've tried passing it to an array, to a list, just can't seem to get it to give the result, just the identifier. 我试过将它传递给数组,列表,似乎无法让它给出结果,只是标识符。 Any help? 有什么帮助吗? Where am I going wrong? 我要去哪里错了?

What you have at the moment is an IQueryable<T> for some unpronounceable anonymous-type T . 您目前拥有的是一个IQueryable<T>用于一些不可发音的匿名类型T I assume , since you are using text-boxes, that you expect at most one row, so: 假设 ,因为您使用的是文本框,所以最多只能期待一行,因此:

var row = selected2.FirstOrDefault();
if(row == null) {
    textBox1.Text = textBox2.Text = "n/a";
} else {
    textBox1.Text = row.branchCode;
    textBox2.Text = row.branchName;
}

Since you use textbox this mean you query one row: 由于使用文本框,这意味着您查询一行:

var onerow= selected2.FirstOrDefault();

if(row == null) { textBox1.Text = textBox2.Text = "n/a";} 

else { textBox1.Text = onerow.branchCode; textBox2.Text = onerow.branchName; }

Hope this help you 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM