简体   繁体   English

从C#连接到MS Access

[英]Connecting to MS Access from C#

I found this code online, which connects from C# to an SQL server database. 我在网上找到了此代码,该代码从C#连接到SQL Server数据库。

I wish to do something similar, but I want to connect to an Access 2010 database. 我希望做类似的事情,但是我想连接到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;
            }
        }
    }
}

How should I change my code to do that ? 我应该如何更改代码来做到这一点? For the example, let's assume that my access DB is in: C:\\VisualStudioProject\\Sample 对于该示例,假设我的访问数据库位于:C:\\ VisualStudioProject \\ Sample

Thank you! 谢谢!

You need to do the following: 您需要执行以下操作:

  1. Use Connection string as: 将连接字符串用作:

    string connectionstring = Standard security Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myFolder\\myAccessFile.accdb; 字符串连接字符串=标准安全提供程序= Microsoft.ACE.OLEDB.12.0;数据源= C:\\ myFolder \\ myAccessFile.accdb; Persist Security Info=False; 持续安全信息= False;

  2. Use OLEDB instead of SQL 使用OLEDB代替SQL

    OleDbConnection MyConn = new OleDbConnection(connectionstring); OleDbConnection MyConn =新的OleDbConnection(连接字符串);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM