想要在访问中使用两个相关表填充组合框

时间:2015-05-15 04:58:03

标签: c# visual-studio-2012 ms-access-2010

我的访问数据库中有两个相关的表, Table1 Table2 。 在表1中,我有" Cd_Table1 "(primaryKey)和" Cd_Table2 "(FOREIGNKEY)。 在表2中,我有" Cd_Table2 "(primaryKey)和" Nm_Atr "(字符串)。 我想用这两个表填充一个组合框,但是" DisplayMemberPath "需要" Nm_Atr "和" ValueMemberPath "需要" Cd_Table1 "。

我的代码:

OleDbCommand cmd = new OleDbCommand("SELECT Table1.Cd_AtributosNormais, Table1.Nm_Atr, Table2.Cd_AtributosNormais FROM Table1, Table2 WHERE Table1.Cd_AtributosNormais = Table2.Cd_AtributosNormais", cn);
            OleDbDataReader reader = cmd.ExecuteReader();
            DataTable table = new DataTable();
            table.Load(reader);
            DataRow row = table.NewRow();
            row["Nm_Atr"] = "";
            table.Rows.InsertAt(row, 0);

            this.btCmbAtkBaAtr1.DataContext = table.DefaultView;
            this.btCmbAtkBaAtr1.DisplayMemberPath = "Nm_Atr";
            this.btCmbAtkBaAtr1.SelectedValuePath = "Cd_AtributosNormais";
            cmd.ExecuteNonQuery();
            reader.Close();
            reader.Dispose();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            cn.Close();
            cn.Dispose();
        }

错误在哪里以及为什么组合框不显示任何内容? 我已经打开了一个连接,并且我正在使用C#来编程和访问2010。

1 个答案:

答案 0 :(得分:0)

如果它有帮助,试试这个..

function getbdtRL(bookbtn)
        {
        var bdate=$("#bdate").val();
            var btime=$("#btime").val();

            var now = new Date();
                    var selectedDate=new Date(bdate);
                    var selectedTime=new Date(btime);
                    alert(btime);//returns for example- 2:00
                    alert(selectedTime);//returns Invalid Date
                    alert(selectedTime.toString());//returns Invalid Date
                    alert(selectedTime.toTimeString());//returns Invalid Date
                    alert(selectedTime.toDateString());//returns Invalid Date

            //Date check is working
            if(selectedDate<now)
                    {
                    alert("Selected Date is in Past");
                    }
                    else if(selectedDate>now)
                    {
                        alert("Selected Date is in Future");    
                    }
                    else if(selectedDate==now)
                    {
                        alert("Selected Date is in Present");   
                    }

            //Time Check is not working by selectedTime
                    if(selectedTime<now)
                    {
                    alert("Selected Time is in Past");
                    }
                    else if(selectedTime>now)
                    {
                        alert("Selected Time is in Future");    
                    }
                    else if(selectedTime==now)
                    {
                        alert("Selected Time is in Present");   
                    }

        //Time Check is not working by btime
                    if(btime<now)
                    {
                    alert("Selected Time is in Past");
                    }
                    else if(btime>now)
                    {
                        alert("Selected Time is in Future");    
                    }
                    else if(btime==now)
                    {
                        alert("Selected Time is in Present");   
                    }
    }


    //Date and Time Difference not working
    var date=new Date();
                var tempdate="2015-05-01";
                var d1 = date;//tempdate;//
                //alert("current date d1="+d1);
                var d2 = RLArrBookDateSender;//receiving from db2 database data type is time which is already booked
                //alert("booked date d2="+d2);
                var DateDiff = {

                        inDays: function(d1,d2) {
                            var t2 = d2.getTime();
                            var t1 = d1.getTime();

                            return parseInt((t2-t1)/(24*3600*1000));
                        }
                };
                alert("diff="+DateDiff.inDays(d1,d2));//no alert executes