简体   繁体   English

如何运行通过C#在Access中创建表的查询?

[英]How do I run a query that makes table in Access through C#?

I need to run query called "MyQuery" that I have in Access that makes a table. 我需要运行Access中创建表的名为“ MyQuery”的查询。

How do I run this query in C# code ? 如何使用C#代码运行此查询?

Have a look at this thread on vbCity , which seems to be exactly about your problem. 看一下vbCity上的该线程 ,它似乎与您的问题有关。

Your code might then look similar to this: 然后,您的代码可能类似于以下内容:

using System.Data;
using System.Data.

using (IDbConnection conn = new OleDbConnection(...)) // <- add connection string
{
    conn.Open();
    try
    {
        IDbCommand command = conn.CreateCommand();

        // option 1:
        command.CommandText = "SELECT ... FROM MyQuery";

        // option 2:
        command.CommandType = CommandType.TableDirect;
        command.CommandText = "MyQuery";

        // option 3:
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "MyQuery";

        using (IDataReader reader = command.ExecuteReader())
        {
            // do something with the result set returned by reader...
        }
    }
    finally
    {
        conn.Close();
    }
}

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

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