Programmatically Build Access Query

后端 未结 1 1441
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 04:16

A Microsoft Office Access database contains Tables, Queries, Forms and Reports.

Is it possible to build and save a query in the Access database from C#?

For e

相关标签:
1条回答
  • 2021-01-28 05:04

    If you want to add Querydefinitons to an existing access database you can do so with the Access Interop asssembly.

    Create a new c# project and add a reference to:

    Microsoft Office 12.0 Access database engine Object Library

    (or a version that matches your Office/Access version)

    This code creates a Query in the Access Database for every table in the database to query the count of rows:

            var dbe = new DBEngine();
            var db = dbe.OpenDatabase(@"c:\path\to\your\youraccessdatabase.accdb");
    
            // loop over tables
            foreach (TableDef t in db.TableDefs)
            {
                // create a querydef
                var qd = new QueryDef();
                qd.Name = String.Format("Count for {0}", t.Name);
                qd.SQL = String.Format("SELECT count(*) FROM {0}", t.Name);
    
                //append the querydef (it will be parsed!)
                // might throw if sql is incorrect
                db.QueryDefs.Append(qd);
            }
    
            db.Close();
    
    0 讨论(0)
提交回复
热议问题