我无法理解代码中的问题。我希望当用户选择一个表名时,它将显示在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");
}
}
答案 0 :(得分:2)
快速猜测 -
也许从
开始dt = dbSet.Tables["TableName"];
到
dt = dbSet.Tables[tableName];