Query Microsoft Access MDB Database using LINQ and C#

后端 未结 6 1599
庸人自扰
庸人自扰 2020-11-27 05:02

I have a *.MDB database file, and I am wondering if it is possible or recommended to work against it using LINQ in C#. I am also wondering what some simple examples would lo

6条回答
  •  执笔经年
    2020-11-27 05:13

    I wrote a small sample program to test this out with David's answer. You'll need to make an access database and manually create the DBML for Linq-to-SQL, as you cannot drag 'n drop them.

    Inserts fail, citing Missing semicolon (;) at end of SQL statement. but queries seem to work alright.

    Access database tables for Program

    using System;
    using System.Collections.Generic;
    using System.Data.OleDb;
    using System.IO;
    using System.Linq;
    using Linq2Access.Data;
    
    namespace Linq2Access
    {
        class Program
        {
            static readonly string AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            static readonly string DbPath = Path.Combine(AppPath, "Data", "database.accdb");
            static readonly string DbConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + DbPath + "';Persist Security Info=False;";
    
            static void Main(string[] args)
            {
                if (!File.Exists(DbPath))
                    throw new Exception("Database file does not exist!");
    
                using (OleDbConnection connection = new OleDbConnection(DbConnString))
                using (DataRepositoryDataContext db = new DataRepositoryDataContext(connection))
                {
                    List projects = new List();
                    for (int i = 1; i <= 10; i++)
                    {
                        dbProject p = new dbProject() { Title = "Project #" + i };
                        for (int j = 1; j <= 10; j++)
                        {
                            dbTask t = new dbTask() { Title = "Task #" + (i * j) };
                            p.dbTasks.Add(t);
                        }
                        projects.Add(p);
                    }
    
                    try
                    {
                        //This will fail to submit
                        db.dbProjects.InsertAllOnSubmit(projects);
                        db.SubmitChanges();
                        Console.WriteLine("Write succeeded! {0} projects, {1} tasks inserted",
                                            projects.Count,
                                            projects.Sum(x => x.dbTasks.Count));
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Write FAILED. Details:");
                        Console.WriteLine(ex);
                        Console.WriteLine();
                    }
    
                    try
                    {
                        //However, if you create the items manually in Access they seem to query fine
                        var projectsFromDb = db.dbProjects.Where(x => x.Title.Contains("#1"))
                                                            .OrderBy(x => x.ProjectID)
                                                            .ToList();
    
                        Console.WriteLine("Query succeeded! {0} Projects, {1} Tasks",
                                            projectsFromDb.Count,
                                            projectsFromDb.Sum(x => x.dbTasks.Count));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Query FAILED. Details:");
                        Console.WriteLine(ex);
                        Console.WriteLine();
                    }
    
                    Console.WriteLine();
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
            }
        }
    }
    

提交回复
热议问题