数据库打开都正常,就是没显示数据在datagrid上。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace CustomerEditor
{
    public partial class Form1 : Form
    {
        private OleDbConnection _connection;        //添加连接属性
        public OleDbConnection Connection
        {
            get
            {
                return _connection;
            }
            set
            {
                Disconnection();
                _connection = value;
            }        }
        //断开数据库连接
        public void Disconnection() 
        {
            if (_connection != null)
            {
                if (_connection.State != ConnectionState.Closed)
                    _connection.Close();
                _connection = null;
            }        }        //意外处理
        public void HandleException(string message, Exception EX)
        {
            MessageBox.Show(this, string.Format("{0}\n{1}:{2}", message, EX.GetType().ToString(), EX.Message));        }
        //连接数据库
        public void Connect()
    { 
            if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    string connectionString = string.Format(
                        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",dialogOpenFile.FileName);
                    //Create a connection
                    OleDbConnection newConnection = new OleDbConnection(connectionString);
                    newConnection.Open();
                    Connection = newConnection;
                }
                catch(Exception EX)
                {
                   //
                    HandleException("无法创建数据连接", EX);
                }
            }    }        public Form1()
        {          
            InitializeComponent();
        }        private void menuDataConnect_Click(object sender, EventArgs e)
        {
            Connect();
        }        //装载数据
        public void LoadData()
        {
            //
            if (_connection == null)
            {
                MessageBox.Show(this, "你必须连接一个数据库!");
                return;
            }
            OleDbCommand command = null;
            OleDbDataAdapter adapter = null;
            try
            {
                command = _connection.CreateCommand();
                command.CommandText = "person";
                command.CommandType = CommandType.TableDirect;
                adapter = new OleDbDataAdapter(command);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                datagridCustomers.DataSource = dataset;
            }
            catch (Exception EX)
            {
                HandleException(" 无法装入数据!", EX);
            }
            finally
            {
                if (adapter != null)
                    adapter.Dispose();
                if (command != null)
                    command.Dispose();
            }                        }        private void menuDataLoad_Click(object sender, EventArgs e)
        {
            //装载数据
            //LoadData();
        }
    }
}

解决方案 »

  1.   

    private void menuDataLoad_Click(object sender, EventArgs e)
            {
                //装载数据
                LoadData();
            }这个别用说。
      

  2.   

    command.CommandText = "person";command.CommandText = "SELECT * FROM TABLE";
      

  3.   

    command.CommandText = "person";
    如果person是连接字符串,就不要打引号啊
      

  4.   

    不行的。
    效果应该是在datagrid上显示一个table的,然后点击看到表的内容。。
      

  5.   

    datagridCustomers.DataSource = dataset;
    datagridCustomers.DataMember = dataset.Table[0].TableName;
    datagridCustomers.databind();
      

  6.   

    datagridCustomers.databind();
    没有这个属性的。
    编译通不过。
      

  7.   

    kbxj406(羽儿)
    错误 1 “System.Windows.Forms.DataGridView”并不包含“DataBind”的定义 F:\我的文档\Visual Studio 2005\Projects\CustomerEditor\CustomerEditor\Form1.cs 104 35 CustomerEditor
      

  8.   

    肯定有DataBind()的,你看是哪里写错了,这个方法一定要的
      

  9.   

    winform不用 DataBind()只绑定dataset不成,还要指定表的名字。把datagridCustomers的DataMember 属性指定为person
    datagridCustomers.DataMember = "person";
    datagridCustomers.DataSource = dataset;
      

  10.   

    datagridCustomers.SetDataBinding(dataset, dataset.Table[0].TableName)
      

  11.   

    因为你没有绑定数据
    datagridCustomers.databind();
      

  12.   

    1.跟踪看看Open数据库是否正确
    2.跟踪Select语句是否正确,可以把它到查询分析器里Run一下
    3.把 datagridCustomers.DataSource = dataset;
      改成: datagridCustomers.DataSource = dataset.Table[0];
      

  13.   

    楼主用的是winform,为什么还有人没看清楚
      

  14.   

    command = _connection.CreateCommand();
                    command.CommandText = "select * from person";
                     adapter = new OleDbDataAdapter(command);...这样也不行。
      

  15.   

    你調試下,檢查Dataset中有沒數據,應該沒有,查看報錯信息!
      

  16.   

    加个Databind()方法,该方法是DataGrid自带的!
      

  17.   

    靠,没看到,原来是FORM的,说错了!
      

  18.   

    首先看看表名是否正确,另外DataMember要加上。
    如果不行,先看DataSet中是否有数据,如果没,则先用Execute()看下是否有返回结果!
      

  19.   

    //连接数据库
            public void Connect()
            {
                if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        string connectionString = string.Format(
                            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;", dialogOpenFile.FileName);
                        //Create a connection
                        OleDbConnection newConnection = new OleDbConnection(connectionString);
                        newConnection.Open();
                        Connection = newConnection;
                    }
                    catch (Exception EX)
                    {
                        //
                        HandleException("无法创建数据连接", EX);
                    }
                }        }这里有没有错? 谢谢。
      

  20.   

    为什么Dataset中没有数据?
    我的Access里有2个表的,应该来说会显示表person的啊 。
      

  21.   

    public void LoadData()
            {
                //
                if (_connection == null)
                {
                    MessageBox.Show(this, "你必须连接一个数据库!");
                    return;
                }
                OleDbCommand command = null;
                OleDbDataAdapter adapter = null;
                try
                {
                    command = _connection.CreateCommand();
                    command.CommandText = "telbooks";//这里如果我表名改不存在的就提示表错,说明可以取得指定的表啊 。
                    command.CommandType = CommandType.TableDirect;
                    adapter = new OleDbDataAdapter(command);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    datagridTelBook.DataSource = dataset;
                }
                catch (Exception EX)
                {
                    HandleException(" 无法装入数据!", EX);
                }
                finally
                {
                    if (adapter != null)
                        adapter.Dispose();
                    if (command != null)
                        command.Dispose();
                }                        }奇怪了,Datagrid就是不提示表名。