Microsoft jet数据库引擎找不到表

时间:2011-02-17 14:01:54

标签: c# jet

我将拼写mbd更正为mdb,但现在错误是

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)
        {

        }
    }

2 个答案:

答案 0 :(得分:1)

您是否需要在连接字符串中提供文件的完整路径?

答案 1 :(得分:1)

蒂姆和比利尔是对的,你需要提供完整的路径。我测试了你的代码,当你添加完整路径

时它就可以工作了
       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();

    }