C#绑定数据库,通过comboBox显示数据,为什么编译能通过,但不能运行啊?
我初学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 ( )) ;
}
}