C#_Access连接问题

 1、找不到可安装的 isam

 2、无法启动应用程序。工作组信息文件丢失,或是已被其他用户以独占方式打开

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DataBase练习
{
public partial class frmConnectAccess : Form
{
public frmConnectAccess()
{
InitializeComponent();
}

/// <summary>
/// 连接数据源
/// </summary>
/// <param name="UserName">用户名</param>
/// <param name="PassWord">密码</param>
/// <returns>是否连接成功</returns>
private bool GetConnAccess(string UserName, string PassWord)
{
bool boo = false;

try
{
string strPath = Application.StartupPath + @"\Access.mdb";
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;Persist Security Info=true;Jet OLEDB:Database Password = '" + PassWord + "';User ID = " + UserName + ";Data Source = " + strPath;

OleDbConnection oleCon = new OleDbConnection(connStr);
OleDbDataAdapter oleDap = new OleDbDataAdapter("select * from 帐目", oleCon);
DataSet oleSet = new DataSet();
oleDap.Fill(oleSet, "帐目");
gridAccess.DataSource = oleSet.Tables[0].DefaultView;
oleCon.Close();
oleCon.Dispose();
boo = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return boo;
}

private void btnConnect_Click(object sender, EventArgs e)
{
string UserName = txtUserName.Text.Trim();
string PassWord = txtPassWord.Text.Trim();
bool boo = GetConnAccess(UserName, PassWord);
}
}
}


 

 

错误连接字符串:string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;DataBase PassWord = '"+PassWord+"';User ID = "+UserName+";Data Source = " + strPath;

DataBase PassWord 应写成 Jet OLEDB:Database Password 注意加:Persist Security Info=true;

正确连接字符串:string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;Persist Security Info=true;Jet OLEDB:Database Password = '" + PassWord + "';User ID = " + UserName + ";Data Source = " + strPath;

posted @ 2011-12-07 18:00  摩根船长  阅读(399)  评论(0编辑  收藏  举报