简体   繁体   English

在 C# 的单个查询中加入 Access 表和 SQL Server 表

[英]JOIN an Access table and a SQL Server table in a single query from C#

I would like to use c# to select and display data from an SQL Server and a MS Access Database (accdb).我想使用 c# 从 SQL Server 和 MS Access 数据库 (accdb) 中选择和显示数据。

Eg例如

SELECT sqlserverTable.id , sqlserverTable.data, accessTable.info 
FROM sqlserverTable 
INNER JOIN accessTable on accessTable.id =  sqlserverTable.id

Current approach to connect to the SQL Server in c# uses:当前在 c# 中连接到 SQL Server 的方法使用:

string connectionString = "Data Source=SQLDATA;Initial Catalog=P22;
    User ID=<user>;Password=<pw>";
SqlConnection cnn = new SqlConnection(connectionString);

and Access:和访问:

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
    Data Source=\\network\data\access.accdb";
OleDbConnection aCnn = new OleDbConnection(connectionString);

I have read I could use linked server (sp_addlinkedserver).我读过我可以使用链接服务器 (sp_addlinkedserver)。 But I do only have read access to the SQLServer.但我只有对 SQLServer 的读取权限。 I would love to use ac# only solution.我很想使用 ac# only 解决方案。

Is there a way to run a Join from multiple data connections?有没有办法从多个数据连接运行连接?

Best wishes, Hakuin最好的祝福,白隐


I tried OPENROWSET as suggested by @CrnaStena but only got as far as我按照@CrnaStena 的建议尝试了 OPENROWSET,但只达到了

cnn = new SqlConnection(connectionString);
cnn.Open();
SqlCommand command = new SqlCommand(" SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'D:\\access.accdb';'admin';'' ,  Tabelle1) As ors ");

And then trying to get around a 'Microsoft.ACE.OLEDB.12.0' not registered error.然后试图绕过“Microsoft.ACE.OLEDB.12.0”未注册错误。 There are plenty of solutions for this recommending to install "Access Database Engine 2007/2010 in 32/64bit " and "Office Data Connectivity Components".有很多解决方案建议安装“Access Database Engine 2007/2010 in 32/64bit”和“Office Data Connectivity Components”。 But so far it did not resolve the error message.但到目前为止它没有解决错误信息。 Also to compile in x86 mode only.也只能在 x86 模式下编译。 So currently I'm stuck here.所以目前我被困在这里。

Based on the other comments: The dataset is rather small.基于其他评论:数据集相当小。 Less than 10.000 rows are affected and those in most cases reduced using where statments to display < 100 rows.少于 10.000 行受到影响,并且在大多数情况下减少了使用 where 语句显示 < 100 行。

I would look into OPENROWSET , here is documentation .我会研究OPENROWSET这里是文档 Then you could do something like this:然后你可以做这样的事情:

USE Northwind  ;  
GO  
SELECT c.*, o.*  
FROM Northwind.dbo.Customers AS c   
   INNER JOIN OPENROWSET('Microsoft.Jet.OLEDB.4.0',   
   'C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb';'admin';'', Orders)      
   AS o   
   ON c.CustomerID = o.CustomerID ;  
GO 

I know this in not with C#, but depending on your table size, I would not try to pull into C# 10 millions rows just to do join on another table of 10 million rows.我知道这与 C# 无关,但是根据您的表大小,我不会尝试在 C# 中加入 1000 万行只是为了连接另一个 1000 万行的表。 So, if your tables have 50 rows great, otherwise look for DB solutions.因此,如果您的表有 50 行,否则请寻找 DB 解决方案。

The Access Database Engine should be able to join both tables if you use the [ODBC; ...]如果您使用[ODBC; ...] [ODBC; ...] syntax to reference the SQL Server table. [ODBC; ...]语法来引用 SQL Server 表。 I just tried the following and it worked for me:我只是尝试了以下方法,它对我有用:

connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\Database1.accdb";
using (var conn = new OleDbConnection(connStr))
{
    conn.Open();
    string sql = 
            @"SELECT " +
            @"    sqlserverTable.id, " +
            @"    accessTable.[Last Name] AS lname_a, " +
            @"    sqlserverTable.last_name AS lname_s " +
            @"FROM " +
            @"    Donor AS accessTable " +
            @"    INNER JOIN " +
            @"    [ODBC;DRIVER=SQL Server;SERVER=.\SQLEXPRESS;DATABASE=myDb;Trusted_Connection=yes].Donor AS sqlserverTable " +
            @"        ON accessTable.[Donor ID] = sqlserverTable.id";
    using (var cmd = new OleDbCommand(sql, conn))
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            Console.WriteLine("{0} | {1}", rdr["lname_a"], rdr["lname_s"]);
        }
    }
}

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

相关问题 C# LINQ select from table and join multiple table and join a SQL query view in SQL server - C# LINQ select from table and join multiple table and join a SQL query view in SQL server Use inner join with C# SQL Server (Retrieving data from multiple table using SQL query and inner join in C# ADO.NET) - Use inner join with C# SQL Server (Retrieving data from multiple table using SQL query and inner join in C# ADO.NET) 从SQL Server C#LINQ查询到一个表 - C# linq query from sql server into one table 使用c#将MS Access表数据添加到SQL Server表 - Add MS Access table data to SQL Server table with c# c#尝试加入表时出现SQL查询错误 - c# SQL Query error when trying to join a table 将“ INNER JOIN”查询从Access移到C#和SQL - Moving “INNER JOIN” Query from Access to C# and SQL 使用C#从SQL Server中的单个表在ASP.NET中创建多个RadioButtonLists - Create multiple RadioButtonLists in ASP.NET from a single table in SQL Server using C# C#和SQL Server查询返回大表上的空列 - C# & SQL Server query returning null columns on large table C# lambda 将一个表的多个列连接到另一个表的单个列 - C# lambda Join multiple columns from one table to single column from another table 具有多个表联接查询的C#datagridview - C# datagridview with multiple table join query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM