C# 无法使用 opendatasource 和 Microsoft.ACE.OLEDB.16.0 从存储过程中获取数据

C# can't get datas from stored procedure with opendatasource and Microsoft.ACE.OLEDB.16.0

我在从 Eexcel 文件中读取数据时遇到一个奇怪的问题。 我在 SQL 服务器中使用 OPENDATASOURCE:

进行了查询
    SELECT COMPETENZA, [CODICE CLEINTE], [RAGIONE SOCIALE], MODELLO, VARIANTE, 
  TIPOLOGIA, ESSENZA, FINITURA, TAPPEZZERIA, COMMESSA, ROUND([% MOL SU RICAVI NETTI] * 100,2) as MARGINE
  FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.16.0', 
   'Data Source=K:\UTENTI\SAMUELE\temp id commesse\ID Commesse produzione.xls;Extended Properties=EXCEL 12.0')...['Consuntivi produzione$']
  WHERE MODELLO IS NOT NULL AND [% MOL SU RICAVI NETTI] IS NOT NULL

如果我在存储过程中执行这个查询,它工作正常。

但是当我通过 HttpGet 从 C# 执行时,我遇到了这个问题:

System.Data.SqlClient.SqlException (0x80131904): Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.16.0" for linked server "(null)".

这是我的 C# 代码:

[HttpGet]
    public async Task<JsonResult> GetMarginiCommessaListJSON()
    {
        var yourdata = GetMarginiCommessaList();

        return Json(new { data = yourdata });
    }

    public List<MarginiCommessa> GetMarginiCommessaList()
    {
        SqlDataReader reader;
        SqlConnection sqlConn = new SqlConnection(_configuration["dbConnectionString"]);
        string sqlCmdStr = "MARGINI_COMMESSE";
        SqlCommand sqlCmd = new SqlCommand(sqlCmdStr, sqlConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        List<MarginiCommessa> allestList = new List<MarginiCommessa>();

        try
        {
            using (sqlConn)
            {
                sqlConn.Open();
                reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                {
                    allestList.Add(new MarginiCommessa
                    {
                        anno = reader.GetValue(0).ToString(),
                        codCli = reader.GetValue(1).ToString(),
                        nomCli = reader.GetValue(2).ToString(),
                        modello = reader.GetValue(3).ToString(),
                        variante = reader.GetValue(4).ToString(),
                        tipologia = reader.GetValue(5).ToString(),
                        essenza = reader.GetValue(6).ToString(),
                        finitura = reader.GetValue(7).ToString(),
                        tapezzeria = reader.GetValue(8).ToString(),
                        commessa_qta = reader.GetValue(9).ToString(),
                        margine = reader.GetValue(10).ToString()
                    });
                }
                reader.Close();
                sqlConn.Close();
            }

        }
        catch (Exception e)
        {
            System.IO.File.WriteAllText(GlobalVariables.errorFolderLocation + "GetMarginiCommessaListJSON.txt", e.ToString());
        }

        return allestList;
    }

这是我的存储过程:

ALTER PROCEDURE [dbo].[MARGINI_COMMESSE]
AS
BEGIN
    SET NOCOUNT ON;

  SELECT COMPETENZA, [CODICE CLEINTE], [RAGIONE SOCIALE], MODELLO, VARIANTE, 
  TIPOLOGIA, ESSENZA, FINITURA, TAPPEZZERIA, COMMESSA, ROUND([% MOL SU RICAVI NETTI] * 100,2) as MARGINE
  FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.16.0', 
   'Data Source=K:\UTENTI\SAMUELE\temp id commesse/ID Commesse produzione.xls;Extended Properties=EXCEL 12.0')...['Consuntivi produzione$']
  WHERE MODELLO IS NOT NULL AND [% MOL SU RICAVI NETTI] IS NOT NULL
  
END

我真的不明白为什么在 SQL 服务器中它工作正常而在 C# 中却不行。 我该如何解决这个问题?

OP #1:

I have a strange issues reading datas from an excel file. I made a query in mssql with OPENDATASOURCE: If i execute this query in a stored procedure, it works fine.

OP #2:

But when i execute from c# by an HttpGet i get this issues:

首先,这是一个苹果和橘子的比较。一方面,您使用的是 MySQL,另一方面是 SQL 服务器。在后一种情况下,您还有一个网络应用程序使用 ADO.NET 来访问存储过程。

现在我不能保证 MySQL 或 OPENDATASOURCE 在这两种情况下所做的任何事情,但有一件事是肯定的 - 您似乎在服务器设置中使用 ACE OLEDB“Jet”可能是你的问题的原因。除了崩溃,使用 ACE 可能会遇到许可问题。见下文。

微软(我的重点):

Although such programmatic development can be implemented on a client system with relative ease, a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET, DCOM, or a Windows NT service.

NOTE: In this context, the Access Database Engine Redistributable and Access Runtime are considered Microsoft Office components.

...和:

Developers who try to use Office in a server-side solution need to be aware of five major areas in which Office behaves differently than anticipated because of the environment.

...最重要的是(我强调):

Besides the technical problems, you must also consider licensing issues. Current licensing guidelines prevent Office applications from being used on a server to service client requests, unless those clients themselves have licensed copies of Office. Using server-side Automation to provide Office functionality to unlicensed workstations is not covered by the End User License Agreement (EULA).

结论

因此,即使您解决了技术问题,也要注意最后一点。

另见