无法将选定的表数据发送到DataTable

时间:2012-05-17 16:20:35

标签: c# datagridview datatable

我无法理解代码中的问题。我希望当用户选择一个表名时,它将显示在datagridView上。但我无法将数据发送到dataTable。当我调试时,dt变量为null。我错过了什么?

这是我到目前为止编写的代码:

        public void ShowSelectedTable(string tableName, DataGridView grid, string   dbFilePath) 
    {
        try
        {
            OleDbConnection dbConn = new OleDbConnection();
            dbConn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbFilePath;
            dbConn.Open();

            dbSet = new DataSet();
            dbAdapt = new OleDbDataAdapter();
            dbAdapt.SelectCommand = new OleDbCommand("Select * From " + tableName + ";", dbConn);
            dbAdapt.Fill(dbSet);
            dt = dbSet.Tables["TableName"];
            grid.DataSource = dt;
        }
        catch(Exception ex)
        {
            MessageBox.Show("error");
        }
    }

1 个答案:

答案 0 :(得分:2)

快速猜测 -

也许从

开始
dt = dbSet.Tables["TableName"]; 

dt = dbSet.Tables[tableName];