我初学C#绑定ACCESS数据库中表的某个字段,比如绑顶comboBox控件,编译能通过,但是确说An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
怎么回事啊,弄半天都不行!!!!急死了!!!!
程序代码如下:
public class Form1 : Form
{
    private ComboBox ComboBox1 ;
    private Button button1 ;
    private System.Data.DataSet myDataSet ;
    private System.ComponentModel.Container components = null ;
    
    public Form1 ( )
    {
        file://打开数据链接,得到数据集
        GetConnect ( ) ;
        InitializeComponent ( ) ;
    }
    file://清除程序中使用过的资源
    protected override void Dispose (bool disposing)
    {
        if (disposing)
        {
            if (components != null) 
            {
                components.Dispose ( ) ;
            }
        }
        base.Dispose (disposing) ;
    }
    
    private void GetConnect ( )
    {
        file://创建一个 OleDbConnection
        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
        OleDbConnection myConn = new OleDbConnection (strCon) ;
        string strCom = " SELECT * FROM person " ;
        file://创建一个 DataSet
        myDataSet = new DataSet ( ) ;
        
        myConn.Open ( ) ;
        file://用 OleDbDataAdapter 得到一个数据集
        OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
        file://把Dataset绑定person数据表
        myCommand.Fill (myDataSet , "person") ;
        file://关闭此OleDbConnection
        myConn.Close ( ) ;
        
    }
    
    private void button1_Click (object sender , System.EventArgs e)
    {
        ComboBox1.DataSource = myDataSet ;
        ComboBox1.DisplayMember = "person.xm" ;
        ComboBox1.ValueMember = "person.xm" ;
    }
    static void Main ( ) 
    {
        Application.Run (new Form1 ( )) ;
    }
}

解决方案 »

  1.   

    ComboBox1.DataSource = myDataSet.Tables[0];
      

  2.   

    1、
    change
    string strCom = " SELECT * FROM person " ;
    with
    string strCom = " SELECT * FROM [person] " ;2
    change
    ComboBox1.DataSource = myDataSet ;
    ComboBox1.DisplayMember = "person.xm" ;
    ComboBox1.ValueMember = "person.xm" ;
    with
    ComboBox1.DataSource = myDataSet.Tables["person"];
    ComboBox1.DisplayMember = "xm" ;
    ComboBox1.ValueMember = "xm" ;
      

  3.   

    出现黄!,An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll.
    下面这句话是绿色字体
    OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,myConn);
      

  4.   

    你的myConn有问题,连接字符串有问题,看看对应的文件是否存在。
      

  5.   

    你再测试一下吧!
    public class Form1 : Form
    {
        private ComboBox ComboBox1 ;
        private Button button1 ;
        private System.Data.DataSet myDataSet ;
        private System.ComponentModel.Container components = null ;
        
        public Form1 ( )
        {
            file://打开数据链接,得到数据集
            GetConnect ( ) ;
            InitializeComponent ( ) ;
        }
        file://清除程序中使用过的资源
        protected override void Dispose (bool disposing)
        {
            if (disposing)
            {
                if (components != null) 
                {
                    components.Dispose ( ) ;
                }
            }
            base.Dispose (disposing) ;
        }
        
        private void GetConnect ( )
        {
            file://创建一个 OleDbConnection
            string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
            OleDbConnection myConn = new OleDbConnection (strCon) ;
            string strCom = " SELECT * FROM person " ;
            file://创建一个 DataSet
            myDataSet = new DataSet ( ) ;
            
            myConn.Open ( ) ;
            file://用 OleDbDataAdapter 得到一个数据集
            OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom , myConn) ;
            file://把Dataset绑定person数据表
            myCommand.Fill (myDataSet) ;////////
            file://关闭此OleDbConnection
            myConn.Close ( ) ;
            
        }
        
        private void button1_Click (object sender , System.EventArgs e)
        {
            ComboBox1.DataSource = myDataSet.Table[0].DefaultView ;////////
            ComboBox1.DisplayMember = "xm" ;//////////
            ComboBox1.ValueMember = "xm" ;///////////
        }
        static void Main ( ) 
        {
            Application.Run (new Form1 ( )) ;
        }
    }
      

  6.   

    楼上的谢谢你,刚才的问题解决了,但是这句话又出现了绿色框框,怎么回事啊
    myCommand.Fill (myDataSet) ;////////
      

  7.   

    谢谢Knight94(愚翁),可真是大好人啊! 还有楼上的兄弟们
      

  8.   

    绑定的有问题,
    ComboBox1.DataSource = myDataSet ;//数据集
    ComboBox1.DisplayMember = "person.userId" ;//表名加字段名
    ComboBox1.ValueMember = "person.username" ;this.ComboBox1.DataSource = myDataTable;//数据表
    this.ComboBox1.DisplayMember = "Name";//字段名
    this.ComboBox1.ValueMember = "userId";//字段名