从C#连接到MS Access

时间:2014-05-27 10:14:48

标签: c# ms-access-2010

我在网上找到了这个代码,它从C#连接到SQL服务器数据库。

我希望做类似的事情,但我想连接到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;
            }
        }
    }
}

我应该如何更改代码呢? 例如,让我们假设我的访问数据库位于:C:\ VisualStudioProject \ Sample

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

  1. 使用连接字符串:

    string connectionstring =标准安全性 Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ myFolder \ myAccessFile.accdb; 坚持安全信息=错误;

  2. 使用OLEDB代替SQL

    OleDbConnection MyConn = new OleDbConnection(connectionstring);

答案 1 :(得分:0)

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;
            }
        }
    }