2011-06-29 97 views
0

我不確定,但我無法獲取要顯示在文本框中的數據。這是我迄今爲止編寫的代碼。任何幫助都會很棒。盒子裏沒有任何東西,但是做了測試,我得到了消息框。有什麼我不正確的?如果需要,我可以提供訪問文件。但是,它只是五個領域的數據。來自訪問db文件的數據

DataSet DataSet1; //use to put data in form 

System.Data.OleDb.OleDbDataAdapter dataadapter; 

private void Breed_Load(object sender, EventArgs e) 
{ 
    dbconnect = new System.Data.OleDb.OleDbConnection();//database connection variable 
    DataSet1 = new DataSet(); //variable to help get info from DB 

    dbconnect.ConnectionString = "PROVIDER= Microsoft.Jet.OLEDB.4.0; Data Source=C:/Pets.mdb"; //location of DB to open 

    dbconnect.Open(); //open command for DB 

    string sql = "SELECT * From tblPets"; //sql string to select all records from the table pets 
    dataadapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbconnect); // pulls the records from sql command 

    MessageBox.Show("Database is Open"); 

    dataadapter.Fill(DataSet1, "Pets"); // used the database to fill in the form. 
    NavRecords(); //calls NavRecords Method 

    dbconnect.Close(); 

    MessageBox.Show("Database is Closed"); 

    dbconnect.Dispose(); 


} 


private void NavRecords() 
{ 
    DataRow DBrow = DataSet1.Tables["Pets"].Rows[0]; 

    //PetNametextBox.Text = DBrow.ItemArray.GetValue(1).ToString(); //puts data in textbox 
    TypeofPettextBox.Text = DBrow.ItemArray.GetValue(1).ToString();//puts data in textbox 
    PetWeighttextBox.Text = DBrow.ItemArray.GetValue(2).ToString();//puts data in textbox 
    ShotsUpdatedtextBox.Text = DBrow.ItemArray.GetValue(3).ToString();//puts data in textbox 
    AdoptabletextBox.Text = DBrow.ItemArray.GetValue(4).ToString();//puts data in textbox 
    BreedtextBox.Text = DBrow.ItemArray.GetValue(5).ToString();//puts data in textbox 
} 
+0

爲什麼不只是'DBrow [0]/DBrow [1] ...' – Rahul

+1

設置一個斷點並檢查該行是否實際包含數據。考慮使用「DBrow [0] .ToString()」,你也應該考慮使用DataGridView。 – MrFox

+0

我設置了一個斷點,沒有看到任何事情。但我不是100%在哪裏看。 UGH –

回答

2

從Access數據庫提取數據非常簡單。這裏有一個例子:

public static DataTable GetBySQLStatement(string SQLText) 
{ 
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(); 
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Pets.MDB"; 
    System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(); 

    Conn.ConnectionString = ConnectionString; 
    cmd.CommandType = CommandType.Text; 
    cmd.Connection = Conn; 
    cmd.CommandText = SQLText; 

    DataSet ds; 
    System.Data.OleDb.OleDbDataAdapter da; 
    DataTable Table = null; 

    Conn.Open(); 
    da = new System.Data.OleDb.OleDbDataAdapter(); 
    da.SelectCommand = cmd; 
    ds = new DataSet(); 
    da.Fill(ds); 

    if (ds.Tables.Count > 0) 
     Table = ds.Tables[0]; 
    Conn.Close(); 
    return Table; 
} 

你調用這個函數像這樣:

DataTable dt = GetBySQLStatement("SELECT * FROM tblPets"); 

if (dt != null) { 
    // If all goes well, execution should get to this line and 
    // You can pull your data from dt, like dt[0][0] 
} 

唯一的「疑難雜症」需要注意的是,這個代碼必須被編譯爲32位應用程序,因爲有沒有64位Jet驅動程序。默認情況下,Visual Studio將編譯爲混合的32位和64位程序。更改項目設置中的選項以確保其僅32位。

+1

沒有64位Jet驅動程序,但可以使用64位版本的ACE(可從MS下載)訪問64位上下文中的MDB文件。 –