简体   繁体   English

通过DSN与SQLServer链接表的MS Access数据库的ODBC连接

[英]ODBC connection to MS Access database with SQLServer linked tables via DSN

So I have a msaccess database with a couple linked tables that point to an SQL Server database via DSN with a username and password built in. 所以我有一个带有几个链接表的msaccess数据库,这些表通过DSN指向SQL Server数据库,内置用户名和密码。

In my .net app I need to connect to my access database, and run queries on the linked tables. 在我的.net应用程序中,我需要连接到我的访问数据库,并在链接表上运行查询。 I have tried with OLEDB and ODBC methods, neither seem to work for the linked tables that point to SQL Server, but linked tables pointing to other access tables are fine. 我尝试使用OLEDB和ODBC方法,似乎既不适用于指向SQL Server的链接表,但指向其他访问表的链接表也没问题。 I get an error such as: ODBC--- connection to 'THISDSN' failed. 我收到一个错误,例如:ODBC ---与'THISDSN'的连接失败。

Is there a way to connect to my access database so that I can simply run queries and it doesn't matter if the linked table is SQL server or just an access table? 有没有办法连接到我的访问数据库,以便我可以简单地运行查询,如果链接表是SQL服务器或只是一个访问表并不重要?

What you describe can be done, you just need to check some of the details in your setup. 您所描述的内容可以完成,您只需要检查设置中的一些细节。

I have the following Access database with a local table [Expenses] and a linked table [dbo_AccountCodes] 我有以下Access数据库与本地表[费用]和链接表[dbo_AccountCodes]

Expenses.png

dbo_AccountCodes.png

I also have a saved query named [ExpenseDetails] that pulls the data from [Expenses] and uses a JOIN to retrieve the related [AccountDescription]: 我还有一个名为[ExpenseDetails]的保存查询,它从[Expenses]中提取数据并使用JOIN来检索相关的[AccountDescription]:

SELECT Expenses.*, dbo_AccountCodes.AccountDescription
FROM Expenses INNER JOIN dbo_AccountCodes ON Expenses.AccountID = dbo_AccountCodes.AccountID;

I can run that saved Access query from my C# application using the normal OLEDB method: 我可以使用普通的OLEDB方法从我的C#应用​​程序运行保存的Access查询:

static void Main(string[] args)
{
    var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\accounting.accdb;");
    conn.Open();
    var cmd = new OleDbCommand("SELECT * FROM ExpenseDetails", conn);
    OleDbDataReader rdr = cmd.ExecuteReader();
    int rowCount = 0;
    while (rdr.Read())
    {
        rowCount++;
        Console.WriteLine("Row " + rowCount.ToString() + ":");
        for (int i = 0; i < rdr.FieldCount; i++)
        {
            string colName = rdr.GetName(i);
            Console.WriteLine("  " + colName + ": " + rdr[colName].ToString());
        }
    }
    rdr.Close();
    conn.Close();

    Console.WriteLine("Done.");
    Console.ReadKey();
}

When I run it, I get: 当我运行它时,我得到:

Row 1:
  ID: 1
  UserID: dr.evil
  ExpenseDescription: "Laser"
  ExpenseAmount: 1000000
  AccountID: 101
  AccountDescription: Weapons of World Domination
Done.

So it can work. 所以它可以工作。 Some things you could check are: 你可以检查的一些事情是:

  1. Are you using a System DSN for your linked table? 您是否为链接表使用系统DSN? Depending on how your C# code is being executed it may not be able to "see" the DSN if it is of some other type. 根据您的C#代码的执行方式,如果DSN属于其他类型,可能无法“看到”DSN。

  2. If this is an ASP.NET application then make sure that the process under which your code runs has the required credentials to access the SQL Server database. 如果这是一个ASP.NET应用程序,请确保运行代码的进程具有访问SQL Server数据库所需的凭据。 You said that your DSN has "a username and password built in" so make sure that the .Connect string for your linked table explicitly says Trusted_Connection=No; 您说您的DSN“内置了用户名和密码”,因此请确保链接表的.Connect字符串明确显示Trusted_Connection=No; . You could also try re-creating the table link in Access and selecting the "Save password" option in the "Link Tables" dialog. 您还可以尝试在Access中重新创建表链接,并在“链接表”对话框中选择“保存密码”选项。 If you decide to try using Windows Authentication on the SQL Server you may have to add SQL logins on the server and SQL users on the database for NT AUTHORITY\\SYSTEM and/or NT AUTHORITY\\NETWORK SERVICE and/or YourDomain\\YourIisServerName$ , depending on your setup. 如果您决定尝试在SQL Server上使用Windows身份验证,则可能必须在数据库上为NT AUTHORITY\\SYSTEM和/或NT AUTHORITY\\NETWORK SERVICE和/或YourDomain\\YourIisServerName$添加SQL登录服务器和SQL用户,具体取决于在你的设置上。

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

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