繁体   English   中英

从C#连接到MS Access

[英]Connecting to MS Access from C#

我在网上找到了此代码,该代码从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;
            }
        }
    }
}

我应该如何更改代码来做到这一点? 对于该示例,假设我的访问数据库位于:C:\\ VisualStudioProject \\ Sample

谢谢!

您需要执行以下操作:

  1. 将连接字符串用作:

    字符串连接字符串=标准安全提供程序= Microsoft.ACE.OLEDB.12.0;数据源= C:\\ myFolder \\ myAccessFile.accdb; 持续安全信息= False;

  2. 使用OLEDB代替SQL

    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