【问题标题】:C# CLR, not able to connect to excel fileC# CLR,无法连接到excel文件
【发布时间】:2015-03-18 11:46:29
【问题描述】:

我正在尝试从 CLR 过程连接到 xlsx 文件。 我在一个简单的测试器(不是 clr,只是一个控制台应用程序)中运行代码并且它可以工作。 但是当我尝试运行(相同的代码)时,在 SQL 服务管理 2008 中,我收到错误:“未指定的错误”。

这里是c#代码:

 [Microsoft.SqlServer.Server.SqlProcedure]
public static void GetURLandQueueByCLID(SqlString phoneNumber, SqlString isDebug, string wsURL, out SqlInt32 queue, out SqlInt32 priority, out SqlString attData)
{
 string[] parameters = new string[3] { "", "", "" };
        try
        {
            DataTable sheetData = new DataTable();

            string filePath = @"C:\Excel_route_file\ms.xlsx";
            using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;HDR=YES;"))
            {
                Helper.WriteLogToDB("before open");
                conn.Open();

                OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [Sheet1$] where '0575271470' = phone", conn);

                Helper.WriteLogToDB("before fill");
                sheetAdapter.Fill(sheetData);

                parameters[0] = sheetData.Rows[0].ItemArray[1].ToString();
                parameters[1] = sheetData.Rows[0].ItemArray[2].ToString();
                parameters[2] = sheetData.Rows[0].ItemArray[3].ToString();

                conn.Close();
                conn.Dispose();
            }
        }
        catch (Exception ex)
        {
            Helper.WriteLogToDB("fail to fill: " + ex.Message);
        }


        queue = SqlInt32.Parse(parameters [0]);
        priority = SqlInt32.Parse(parameters [1]);
        attData = parameters [2];
 }

它没有到达 conn.Open() :( 这是 OleDbConnection 创建中的内容。

创建过程:

  CREATE PROCEDURE [dbo].[proName]
  (@PHONENUMBER [nvarchar](MAX),@ISDEBUG [nvarchar](MAX),@WSURL [nvarchar] (MAX),@QUEUE INT OUTPUT ,@PRIORITY INT OUTPUT,@ATTDATA [nvarchar](MAX) OUTPUT)

  WITH EXECUTE AS CALLER
  AS

  EXTERNAL NAME [assemblyName].[StoredProcedures].[GetURLandQueueByCLID]
  GO

ASSEMBLY 是使用 PERMISSION_SET = UNSAFE 创建的

我也这样做了:

 ALTER DATABASE MyDatabase SET TRUSTWORTHY ON

还有这个:

 sp_configure 'clr enabled',1
 GO
 RECONFIGURE
 GO
 sp_configure 'clr enabled'  -- make sure it took
 GO

我不能让它工作,帮助。

【问题讨论】:

    标签: c# sql-server excel clr oledbconnection


    【解决方案1】:

    是的,创建 OleDbConnection 时出现问题。在扩展属性的值周围添加引号。

    new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES';")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多