使用访问权限与c#建立数据库连接

时间:2017-03-04 09:49:09

标签: c# ms-access ms-access-2016

我想从c#连接到我的MS Access数据库,我收到以下错误:

  

无法识别的数据库格式'data.accdb'

我该怎么办?请帮忙

3 个答案:

答案 0 :(得分:0)

使用这样的连接字符串:

public static string ConStr = "Provider=Microsoft.ACE.Oledb.15.0; Data Source=D:\db.accdb;";


public static void DeleteTable(string TableName)
{
    OleDbConnection connection = new OleDbConnection(OLEDB.ConStr);
    OleDbCommand oleDbCommand = new OleDbCommand(string.Format("delete from [{0}]", (object) TableName), connection);
    try
    {
       connection.Open();
       oleDbCommand.ExecuteNonQuery();
    }
    catch
    {
    }
    finally
    {
        connection.Close();
    }
}

答案 1 :(得分:0)

我为此做了一堂课。它与最新的视觉工作室完美配合,或与2010年的2010年访问完美配合。如果您有更新版本的访问权限,则需要安装Access Database Engine。它还包含一个函数,它使用sql注入来阻止你的sql被黑客攻击,并创建md5哈希值。

using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Security.Cryptography;

public class Database
{

    #region Variables

    String Name;
    String Path;
    String ConnectionString;
    OleDbConnection Connection;

    #endregion

    #region Init Destroy

    public Database(String Name)
    {
        this.Name = @"App_Data\" + Name;
        Path = HttpContext.Current.Server.MapPath(this.Name);
        ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; " +
                            "Data Source = " + Path + ";" +
                            "Persist Security Info = False;";
        Connection = new OleDbConnection(ConnectionString);
        if (Connection.State == System.Data.ConnectionState.Closed)
        {
            Connection.Open();
        }
    }

    public void finalize()
    {
        if (Connection.State == System.Data.ConnectionState.Open)
        {
            try { Connection.Close(); }
            catch { }
        }
    }

    #endregion

    #region Queries

    public void execute(String query)
    {
        OleDbCommand command = new OleDbCommand(query, Connection);
        command.ExecuteNonQuery();
    }

    public OleDbDataReader select(String query)
    {
        OleDbCommand command = new OleDbCommand(query, Connection);
        OleDbDataReader data = command.ExecuteReader();
        return data;
    }

    public DataSet selectData(String query)
    {
        OleDbCommand command = new OleDbCommand(query, Connection);
        OleDbDataAdapter adp = new OleDbDataAdapter(command);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        return ds;
    }

    public object scalar(String query)
    {
        OleDbCommand command = new OleDbCommand(query, Connection);
        object data = new object();
        data = command.ExecuteScalar();
        return data;
    }

    #endregion

    #region Encryption Security

    public String stripInjection(String field)
    {
        String x = field.Replace(@"'", string.Empty);
        x = x.Replace(@"""", string.Empty);
        return x;
    }

    public string md5Hash(string input)
    {
        StringBuilder hash = new StringBuilder();
        MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
        byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input));

        for (int i = 0; i < bytes.Length; i++)
        {
            hash.Append(bytes[i].ToString("x2"));
        }
        return hash.ToString().ToUpper();
    }

    #endregion

}

不要在类的析构函数中放入finalize指令(正如Microsoft在文档中所说,因为它会启动一些例外)。只需在完成任务后调用它:

Database database = new Database("data.accdb");
DataSet result = database.selectData("SELECT something FROM table WHERE condition;");
database.finalize();

答案 2 :(得分:0)

将您的数据库文件.accdb复制到项目的Debug文件夹中,然后在项目中添加app.config文件

在配置标记

中的app.config中编写以下代码
<connectionStrings>
    <add name="db" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\sse_db.accdb;Persist Security Info=False;"
      providerName="System.Data.Oledb" />
</connectionStrings>

1)添加System.Configuration Reference 使用连接对象如下

OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);