将多个Windows窗体组合框绑定或填充到MS Access数据库

时间:2016-07-31 14:09:07

标签: c# winforms ms-access combobox

我有一个连接到MS Access数据库的winform应用程序。在我的客户端页面上,在我的情况下是Wendy的餐厅,我可以选择餐厅所处的状态,墙壁或屋顶结构是什么,等等。当最终用户在此页面上时,我想要能够从这些各种组合框中进行选择,以选择我的MS数据库文件中的选项,以便添加新记录或更新现有记录。我查看了stackoverflow上的其他帖子,其中作者为一个组合框创建了一个方法&从程序的form_load部分调用它,但我试图将多个组合框填充到我的数据库。我已经在网络上看到了其他人将组合框绑定到数据集的事情。现在我有我的程序的WendysForm_Load部分中定义的一个组合框。为每个组合框创建一个方法更好吗?从我的Form_load部分调用它,还是将所有组合框绑定到我的数据集更好?这是我的代码&我还附上了一张图片:

private void WendysForm_Load(object sender, EventArgs e)//Start of WendysForm_Load code
        {
            // TODO: This line of code loads data into the 'cDG_Project_TypesDataSet.Wendys' table. You can move, or remove it, as needed.
            this.wendysTableAdapter.Fill(this.cDG_Project_TypesDataSet.Wendys);
            try
            {


            connection.Open();//Open Connection
            OleDbCommand command = new OleDbCommand();//Create command object of OleDbCommand
            command.Connection = connection;//This tells OleDbCommand command object to use OleDbConnection connection variable to connect to Access Database
            string query = "SELECT DISTINCT State FROM Wendys";//SQL Statement for Combo Boxes(Still need to figure out how to do multiple Combo Boxes!!!!!!)
            //string query = "SELECT DISTINCT [" + sqlColumnName.ToString() + "] FROM [database].[dbo].[Wendys];";
            //"SELECT DISTINCT AssetType FROM Wendys";
            //"SELECT DISTINCT BaseModel FROM Wendys";
            //"SELECT DISTINCT BldgPrototypeYear FROM Wendys";
            //"SELECT DISTINCT UpgradeClass FROM Wendys";
            //"SELECT DISTINCT ExtWallStructure FROM Wendys";
            //"SELECT DISTINCT NewWallStructure FROM Wendys";
            //"SELECT DISTINCT ExtRoofStructure FROM Wendys";
            //"SELECT DISTINCT NewRoofStructure FROM Wendys";
            //"SELECT DISTINCT ExstFoundationType FROM Wendys";
            //"SELECT DISTINCT NewFoundationType FROM Wendys";
            //"SELECT DISTINCT Addition FROM Wendys";
            //"SELECT DISTINCT BladeDesign FROM Wendys";
            //"SELECT DISTINCT DriveThruType FROM Wendys";
            //"SELECT DISTINCT Patio FROM Wendys";
            //"SELECT DISTINCT TrashEnclosure FROM Wendys";
            //"SELECT DISTINCT CounterStyle FROM Wendys";
            //"SELECT DISTINCT SelfServe FROM Wendys";
            //"SELECT DISTINCT DigitalStatic FROM Wendys";
            //"SELECT DISTINCT RaisedCeiling,Notes FROM Wendys";
            //"SELECT DISTINCT Notes FROM Wendys";


            command.CommandText = query;//This asigns string variable query to command.CommandText object

            OleDbDataReader reader = command.ExecuteReader();//This executes Query
            while (reader.Read())
            {

                //Populate Combo Boxes with Access Database fields(Still need to figure out how to do multiple Combo Boxes!!!!!!)
                cb_State.Items.Add(reader["State"].ToString());
                //    cb_AssetType.Items.Add(reader["AssetType"].ToString());
                //    cb_BaseModel.Items.Add(reader["BaseModel"].ToString());
                //    cb_BldgPrototypeYear.Items.Add(reader["BldgPrototypeYear"].ToString());
                //    cb_UpgradeClass.Items.Add(reader["UpgradeClass"].ToString());
                //    cb_ExtWallStructure.Items.Add(reader["ExtWallStructure"].ToString());
                //    cb_NewWallStructure.Items.Add(reader["NewWallStructure"].ToString());
                //    cb_ExtRoofStructure.Items.Add(reader["ExtRoofStructure"].ToString());
                //    cb_NewRoofStructure.Items.Add(reader["NewRoofStructure"].ToString());
                //    cb_ExtFndType.Items.Add(reader["ExstFoundationType"].ToString());
                //    cb_NewFndType.Items.Add(reader["NewFoundationType"].ToString());
                //    cb_Addition.Items.Add(reader["Addition"].ToString());
                //    cb_BladeDesign.Items.Add(reader["BladeDesign"].ToString());
                //    cb_DriveThruType.Items.Add(reader["DriveThruType"].ToString());
                //    cb_Patio.Items.Add(reader["Patio"].ToString());
                //    cb_TrashEnclosure.Items.Add(reader["TrashEnclosure"].ToString());
                //    cb_CounterStyle.Items.Add(reader["CounterStyle"].ToString());
                //    cb_SelfServe.Items.Add(reader["SelfServe"].ToString());
                //    cb_MenuboardType.Items.Add(reader["DigitalStatic"].ToString());
                //    cb_RaisedCeiling.Items.Add(reader["RaisedCeiling"].ToString());
                //    list_Notes.Items.Add(reader["Notes"].ToString());




            }

            connection.Close();//Close Connection
        }//End of try for WendysForm_Load
        catch (Exception ex)
        {
            MessageBox.Show("Error  " + ex);
        }//end of catch for WendysForm_Load
    }//End of code for WendysForm_Load

My winform called WendysForm is here

My Combo Box Task Dialog box is here

0 个答案:

没有答案