C#连接ACCESS数据库,显示到dataGridView表格?
用C#写了一段程序,想把access数据库的数据显示到dataGridView表格中,下面是一段代码没有报错,就是dataGridView表格中不显示数据,不知道怎么回事?public partial class CustomerEditor : Form
{
private OleDbConnection connection;
public CustomerEditor()
{
InitializeComponent();
}
private void menuDataconnect_Click(object sender, EventArgs e)
{
Connect();
}
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);
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
Connection = newConnection;
}
catch (Exception ex)
{
HandleException("A connection could not be made", ex);
}
}
}
private void HandleException(string p, Exception ex)
{
MessageBox.Show(this, string.Format("{0}\n{1}:{2}", p, ex.GetType().ToString(), ex.Message));
}
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;
}
}
protected void Dispos(bool disposing1)
{
disconnection();
if (disposing1)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing1);
}
private void menuDataload_Click(object sender, EventArgs e)
{
LoadData();
}
public void LoadData()
{
if (connection == null)
{
MessageBox.Show(this, "you must connect to a database");
return;
}
OleDbCommand command = connection.CreateCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
try
{
= "Customers";
= CommandType.TableDirect;
DataSet dataset = new DataSet();
adapter.Fill(dataset);
dataGridCustomers.DataSource = dataset;
}
catch (Exception ex)
{
HandleException("the data could not be loaded", ex);
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
}
}
}
}