从c#连接到MS Access

本文关键字:MS Access 连接 | 更新日期: 2023-09-27 18:05:33

我在网上找到了这段代码,它从c#连接到SQL server数据库。

我希望做类似的事情,但我想连接到Access 2010数据库。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApplication1.DAL
{
    public class PersonDAL
    {
        public string ConString = 
            "Data Source=SOURAV-PC''SQL_INSTANCE;Initial Catalog=test;Integrated Security=True";
        SqlConnection con = new SqlConnection();
        DataTable dt = new DataTable();
        public DataTable Read()
        {
            con.ConnectionString = ConString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            SqlCommand cmd = new SqlCommand("select * from Person",con);
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
            }
            catch
            {
                throw;
            }
        }
        public DataTable Read(Int16 Id)
        {
            con.ConnectionString = ConString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            SqlCommand cmd = new SqlCommand("select * from Person where ID= "+ Id +"", con);
            try
            {
                SqlDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
            }
            catch
            {
                throw;
            }
        }
    }
}

我应该如何修改我的代码来做到这一点?对于示例,让我们假设我的访问DB位于:C:'VisualStudioProject'Sample

谢谢!

从c#连接到MS Access

您需要做以下操作:

  1. 使用连接字符串作为:

    string connectionstring =标准安全性提供者= Microsoft.ACE.OLEDB.12.0;数据源= C: ' myFolder ' myAccessFile.accdb;持久安全信息=False;

  2. 使用OLEDB代替SQL

    OleDbConnection = new OleDbConnection(connectionstring);

public class PersonDAL
    {
        public string ConString =
           @"Standard security Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'myFolder'myAccessFile.accdb; Persist Security Info=False;";
        OleDbConnection con;
        DataTable dt;
        public PersonDAL()
        {
            con = new OleDbConnection();
            dt = new DataTable();
        }
        public DataTable Read()
        {
            con.ConnectionString = ConString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            OleDbCommand cmd = new OleDbCommand("select * from Person", con);
            try
            {
                OleDbDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
            }
            catch
            {
                throw;
            }
        }
        public DataTable Read(Int16 Id)
        {
            con.ConnectionString = ConString;
            if (ConnectionState.Closed == con.State)
                con.Open();
            OleDbCommand cmd = new OleDbCommand("select * from Person where ID= " + Id + "", con);
            try
            {
                OleDbDataReader rd = cmd.ExecuteReader();
                dt.Load(rd);
                return dt;
            }
            catch
            {
                throw;
            }
        }
    }