| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 678 人关注过本帖
标题:[求助]我的程序为什么不能链接到ACCESS数据库?
只看楼主 加入收藏
c_sharp_man
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2005-9-28
收藏
 问题点数:0 回复次数:3 
[求助]我的程序为什么不能链接到ACCESS数据库?

不管打开一个存在的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编辑过]

搜索更多相关主题的帖子: using 数据库 System ACCESS Microsoft 
2006-03-15 09:31
dazhi_
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2006-3-10
收藏
得分:0 

是不是你没安装ACCESS啊?


2006-03-15 09:36
c_sharp_man
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2005-9-28
收藏
得分:0 
装了啊.示例数据库文件也有.

2006-03-15 09:40
c_sharp_man
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2005-9-28
收藏
得分:0 

问题找到了.是语句写错了:(
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB4.0;Data Source = {0}; User ID=;Password=;",DialogOpenFile.FileName);

上句中OLEDB 和4.0之间少了一个".",真是粗心啊.
希望各位不要和我一样哦!


2006-03-15 09:43
快速回复:[求助]我的程序为什么不能链接到ACCESS数据库?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017204 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved