不管打开一个存在的ACCESS数据库文件还是不存在的都提示打不开
错误信息为"末在本地计算机上注册 Microsoft.Jet.OLEDB4.0"提供程序.
源程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace CustomerEditor
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.MainMenu MenuMain;
  private System.Windows.Forms.MenuItem menuData;
  private System.Windows.Forms.MenuItem MenuConnect;
  private System.Windows.Forms.OpenFileDialog DialogOpenFile;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
private OleDbConnection _connection;
  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();
   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }
  public void Connect()
  {
   //显示对话框
   if(DialogOpenFile.ShowDialog(this)==DialogResult.OK) 
   {
    try
    {
     //create a new connection string
     string connectionString = string.Format(
      "Provider=Microsoft.Jet.OLEDB4.0;Data Source = {0}; User ID=;Password=;",DialogOpenFile.FileName);
     //create a connection
     OleDbConnection newConnection = new OleDbConnection(connectionString);
     //Try and open it
     newConnection.Open();
     //Store it for use
     Connection = newConnection;
    }
    catch(Exception ex)
    {
     //report the problem
     HandleException("A conncetion could not be made.",ex); 
     //MessageBox.Show("The file could not be open!");
    }
   }
  }
  //Connection 属性
  public OleDbConnection Connection
  {
   get
   {
    return _connection;
   }
   set
   {
               //Disconnect
                Disconnect();
    _connection = value;
   }
}
  //Disconnect方法
  public void Disconnect()
  {
            //Do we have a connection?
   if( _connection != null )
   {
    //Is it open?
    if( _connection.State !=ConnectionState.Closed )
    {
     _connection.Close();
    }
    //Clear it
    _connection = null;
   }
}
  public void HandleException(string message,Exception ex)
  {
   //Display a message box...
   MessageBox.Show(this,string.Format("{0}\n{1}:{2}",message,ex.ToString(),ex.Message) );
}
          
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   //Do we have a connection?
   Disconnect();
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.MenuMain = new System.Windows.Forms.MainMenu();
   this.menuData = new System.Windows.Forms.MenuItem();
   this.MenuConnect = new System.Windows.Forms.MenuItem();
   this.DialogOpenFile = new System.Windows.Forms.OpenFileDialog();
   // 
   // MenuMain
   // 
   this.MenuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                      this.menuData});
   // 
   // menuData
   // 
   this.menuData.Index = 0;
   this.menuData.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                      this.MenuConnect});
   this.menuData.Text = "&Data";
   // 
   // MenuConnect
   // 
   this.MenuConnect.Index = 0;
   this.MenuConnect.Text = "&Connect...";
   this.MenuConnect.Click += new System.EventHandler(this.MenuConnect_Click);
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Menu = this.MenuMain;
   this.Name = "Form1";
   this.Text = "Customer Editor";
  }
  #endregion
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }
       
  private void MenuConnect_Click(object sender, System.EventArgs e)
  {
   Connect();
  }
}
}
[此贴子已经被作者于2006-3-15 9:32:09编辑过]



 
											





 
	    

 
	
