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 ( ) ) ; } } |