简体   繁体   English

如何在Visual Studio中使用MSSQL数据库数据加载Windows窗体组合框?

[英]How do I load a Windows Forms Combobox, with MSSQL Database data in Visual Studio?

I've been trying different methods of adding MSSQL data to a Windows Forms Combobox for quite some time and have been unable to get it to work. 我一直在尝试将MSSQL数据添加到Windows Forms Combobox的不同方法,但是已经无法正常工作了。 I must be missing something, but I can't figure out what it might be. 我一定想念一些东西,但我不知道可能是什么。

I am trying to add the Description data form a table named Course, in my SQL Database named StudentDatabase. 我试图在名为StudentDatabase的SQL数据库中,从名为Course的表中添加Description数据。

I am using Microsoft SQL Management Studio 2018, and Visual Studio 2017. 我正在使用Microsoft SQL Management Studio 2018和Visual Studio 2017。

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillCombobox();
        }

        void FillCombobox()
        {
            string sql = "SELECT * FROM Course";

            SqlConnection dbConn = new SqlConnection("Data Source = localhost; Initial Catalog = StudentDatabase; Integrated Security = SSPI");
            SqlCommand dbComm = new SqlCommand(sql, dbConn);
            SqlDataReader myReader;

            try
            {
                dbConn.Open();
                myReader = dbComm.ExecuteReader();

                while (myReader.Read())
                {
                    string Desc = myReader["Description"].ToString();
                    comboBox1.Items.Add(Desc);
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show("Error");
            }
        }

    }

}

I expect the Combox box to fill with the different Course Descriptions, yet it remains empty and I receive no error message. 我希望“ Combox”框充满不同的“课程说明”,但它仍然为空,并且没有收到任何错误消息。

Try to make a full list of the items first, then bind them to the ComboBox. 尝试首先列出所有项目,然后将它们绑定到ComboBox。

List<string> _descriptions;

//Fill the list from the database
string query = "SELECT Description FROM Course";
    using(SqlCommand dbComm= new SqlCommand(query, dbConn))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
            _descriptions.Add(reader.GetString(0));
            }         
        }
    }

//Add items to the ComboBox
foreach (string str in _descriptions)
{
    comboBox1.Items.Add(str);
}

//Or add with a datasource without re-iterating
comboBox1.Datasource = _descriptions;

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

相关问题 如何重新同步Visual Studio 2008 Windows窗体设计器? - How do I resync the Visual Studio 2008 Windows Forms Designer? 如何从数据库中将数据加载到组合框中 - How do I load data into combobox from database 如何在Windows窗体中访问组合框项目MemberValue? - How do I access Combobox item MemberValue in windows forms? 如何在Windows窗体组合框中捕获回车键 - How do I capture the enter key in a windows forms combobox 如何使用 Visual Studio 设置 Windows 窗体应用程序的默认文本输入状态? - How do I set the default text input state of a Windows Forms App using Visual Studio? 如何在 Visual Studio 2019 中的 Windows Forms 应用程序中打印变量? - How do I print a variable in a Windows Forms Application in Visual Studio 2019? 如何在 Visual Studio 2019 中添加对新 Windows Forms 项目的引用 - How do I add a Reference to a new Windows Forms Project in Visual Studio 2019 在绘制控件后,如何将光标放回Visual Studio 2010 RC Windows窗体设计器中? - How do I get the cursor back in Visual Studio 2010 RC Windows Forms designer after drawing controls? 如何在Visual Studio 2008中为简单的Windows窗体应用程序插入下拉菜单? - How do I insert a drop-down menu for a simple Windows Forms app in Visual Studio 2008? 如何在 Visual Studio 中附加数据库 - How do I attach a database in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM