参数化存储过程和winforms组合框 [英] parameterized store procedure and winforms comboboxes

查看:65
本文介绍了参数化存储过程和winforms组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的tblSates表中,我有一个名为Monat的列,其中包含这三个值[01.2016,02.2016和03.2016],我想在组合框中获取这些值。



我得到的是价值,但只有两个,而不是全部三个。我调试我得到两个值。

[02.2016和03.2016]

这里我的代码:



In my tblSates Table, I have a column named Monat with these three values [01.2016, 02.2016 and 03.2016] and I want to fetch these values in a combobox.

I am getting the values but just two of them instead of all three. And I debug I stil get two values.
[02.2016 and 03.2016]
Here my code:

private void FillCombobox2()
    {
        string S = ConfigurationManager

        // TSQL-Statement
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");            
        SqlDataReader myReader;
        try
        {
            con.Open();
            myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                DataTable dt = new DataTable();
                dt.Load(myReader);
                combobox1.DisplayMember = "Monat";
                combobox1.ValueMember = "Monat";
                combobox1.DataSource = dt;
                combobox1.SelectedIndex = -1;

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

推荐答案

private void FillCombobox2()
    {
        string S = ConfigurationManager
 
        // TSQL-Statement
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");            
        //SqlDataReader myReader;
        try
        {
            con.Open();
         SqlDataAdapter ad = new SqlDataAdapter(cmd);
         DataTable dt = new DataTable();
                ad.Fill(dt)             
                combobox1.DisplayMember = "Monat";
                combobox1.ValueMember = "Monat";
                combobox1.DataSource = dt;
                combobox1.SelectedIndex = -1;
 
            }
 
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }


这篇关于参数化存储过程和winforms组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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