2011-02-17 66 views
1

我糾正拼寫萬桶至MDB但現在的錯誤是Microsoft Jet數據庫引擎找不到表

Micorsoft Jet數據庫引擎找不到輸入表或查詢員工確保它存在並且其名稱的拼寫正確。

我想要做的是連接到數據庫,並提取四個字段在texbox這裏是我的代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace Empdetails 
{ 
    public partial class EGUI : Form 
    { 

     private OleDbConnection dbConn; // Connectionn object 
     private OleDbCommand dbCmd;  // Command object 
     private OleDbDataReader dbReader;// Data Reader object 
     private Emp1 Edetails; 
     private string sConnection; 
     private string sql; 


     public EGUI() 
     { 

      InitializeComponent(); 
     } 

     private void button1_Click(object sender,System.EventArgs e) 
     { 



      try 
      { 
       // Construct an object of the OleDbConnection 
       // class to store the connection string 
       // representing the type of data provider 
       // (database) and the source (actual db) 
       sConnection = 
        "Provider=Microsoft.Jet.OLEDB.4.0;" + 
        "Data Source=employee.mdb"; 

       dbConn = new OleDbConnection(sConnection); 
       dbConn.Open(); 

       // Construct an object of the OleDbCommand 
       // class to hold the SQL query. Tie the 
       // OleDbCommand object to the OleDbConnection 
       // object 
       sql = "Select * From employee"; 
       dbCmd = new OleDbCommand(); 
       dbCmd.CommandText = sql; 
       dbCmd.Connection = dbConn; 

       // Create a dbReader object 
       dbReader = dbCmd.ExecuteReader(); 

       while (dbReader.Read()) 
       { 
        Edetails = new Emp1 
         (dbReader["Name"].ToString(), dbReader["Address"].ToString(), dbReader["SocialSecurityNumber"].ToString(), dbReader["Rate"].ToString()); 
        textBox1.Text = dbReader["Name"].ToString(); 
        textBox2.Text = dbReader["Address"].ToString(); 
        textBox3.Text = dbReader["SocialSecurityNumber"].ToString(); 
        textBox4.Text = dbReader["Rate"].ToString(); 






        // tb1.Text = dbReader["FirstName"].ToString(); 
       } // tb2.Text = dbReader["LastName"].ToString(); 

       dbReader.Close(); 
       dbConn.Close(); 
      } 
      catch (System.Exception ex) 
      { 
       MessageBox.Show("exeption" + ex.ToString()); 


      } 

     } 

     private void EGUI_Load(object sender, EventArgs e) 
     { 

     } 
    } 

回答

1

Tim和binil是正確的,你需要提供完整的路徑。我測試了你的代碼,它在你添加完整路徑時起作用

 try 
     { 
      // Construct an object of the OleDbConnection 
      // class to store the connection string 
      // representing the type of data provider 
      // (database) and the source (actual db) 
      string sConnection = 
       "Provider=Microsoft.Jet.OLEDB.4.0;" + 
       "Data Source=C:\\Code\\StackOverflowSamples\\ReadFromAccessDB\\employee.mdb"; 

      using (OleDbConnection dbConn = new OleDbConnection(sConnection)) 
      { 
       dbConn.Open(); 

       // Construct an object of the OleDbCommand 
       // class to hold the SQL query. Tie the 
       // OleDbCommand object to the OleDbConnection 
       // object 
       string sql = "Select * From employee"; 
       OleDbCommand dbCmd = new OleDbCommand(); 
       dbCmd.CommandText = sql; 
       dbCmd.Connection = dbConn; 

       // Create a dbReader object 
       using (OleDbDataReader dbReader = dbCmd.ExecuteReader()) 
       { 

        while (dbReader.Read()) 
        { 
         Console.WriteLine(dbReader["EmployeeName"].ToString()); 
         Console.WriteLine(dbReader["Address"].ToString()); 
         Console.WriteLine(dbReader["SSN"].ToString()); 
         Console.WriteLine(dbReader["Rate"].ToString()); 
        } 

       } 
      } 
     } 
     catch (System.Exception ex) 
     { 
      Console.WriteLine("exeption" + ex.ToString()); 


     } 
     Console.ReadLine(); 

    } 
1

你需要提供在連接字符串的完整路徑文件?

+1

正確...需要完整路徑才能找到源代碼。 OleDB不一定基於您運行的目錄。 – DRapp 2011-02-17 14:27:47

相關問題