SQLCommand用于选择同一行但不同列的值 [英] SQLCommand for selecting value in the same row but different column

查看:116
本文介绍了SQLCommand用于选择同一行但不同列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我已经得到了下面的代码,它应该在SQLCommand选择从表busnaz中选择的列,并选择pocsed的值,它与combobox的选定值在同一行中(在nazev列中)。并将此值插入文本框klimax。



我希望有人能帮我找到错误,谢谢!



Hello guys, I have got this code below which should on SQLCommand Select column pocsed from table busnaz, and select the value of pocsed which is in the same row as selected value of combobox(which is in column nazev). And insert this value into textbox klimax.

I hope someone will help me find the error, thanks!

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


            try
            {
                SqlCommand novahodnota = new SqlCommand("...", spojeni);
                spojeni.Open();
                SqlDataReader precti = novahodnota.ExecuteReader();

                precti.Read();
                zname.Text = precti.GetDecimal(2).ToString();
                spojeni.Close();
                
            }
            catch (Exception ex)
            {
                //something
            }
        }

推荐答案

考虑到您的SELECT只返回一列,您对 GetDecimal(2)的调用假设返回至少3列,编号为0,你正试图从结果集中不存在的列返回一个值。
Considering your SELECT is only returning one column, your call to GetDecimal(2) is assuming that there is at least 3 columns being returned, numbered 0, 1 and 2. You're trying to return a value from a column that doesn't exist in the resultset.


我不是100%肯定,但我会将SqlCommond更改为:



SELECT来自busnaz WHERE nazev =+ comboBox1.SelectedIndex但是我不确定Nazev是什么?



else ComboBox1.SelectedValue?
I am not 100% sure on this, but I would change SqlCommond to:

"SELECT pocsed FROM busnaz WHERE nazev=" + comboBox1.SelectedIndex But I am not sure what nazev is?

else ComboBox1.SelectedValue?


这篇关于SQLCommand用于选择同一行但不同列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆