using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
namespace Database
{
public partial class Form1 : Form
{
private OleDbConnection _Connection;
public Form1()
{
InitializeComponent();
}
private void Connect()
{
if (this .openFileDialog1 .ShowDialog ()==DialogResult .OK )
try
{
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; User ID=;PassWord=;", this.openFileDialog1.FileName);
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
_Connection = newConnection;
MessageBox.Show("打开成功!");
}
catch (Exception ex)
{
HandleException("A Connect could not be made", ex);
}
}
public void HandleException(string message, Exception ex)
{
MessageBox.Show(this,string .Format ("{0}\n{1}:{2}",message ,ex .GetType ().ToString (),ex .Message ));
}
public OleDbConnection Connection
{
get
{
return _Connection;
}
set
{
Disconnect();
_Connection = value;
}
}
public void Disconnect()
{
if (_Connection != null)
{
if (_Connection.State != ConnectionState .Closed )
{
_Connection.Close();
_Connection = null;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void connectToolStripMenuItem_Click(object sender, EventArgs e)
{
Connect();
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadData();
}
public void LoadData()
{
if (_Connection == null)
{
MessageBox.Show(this," you must connect database");
return;
}
OleDbCommand Command = null;
OleDbDataAdapter Adapter = null;
try
{
Command = _Connection.CreateCommand();
Command.CommandText = "Customers";
Command.CommandType = CommandType.TableDirect;
Adapter = new OleDbDataAdapter(Command);
DataSet dataset = new DataSet();
Adapter.Fill(dataset);
this.dataGridView1.DataSource = dataset;
MessageBox.Show("it is made");
}
catch (Exception ex)
{
HandleException("The data could not load", ex);
}
finally
{
if (Adapter != null)
{
Adapter.Dispose();
}
if (Command != null)
{
Command.Dispose();
}
}
}
}
}
各位帮我看哈,问题出在哪?都没有看出来的!我就纳闷数据怎么加载不上去呢?