C# CLR,无法连接到 excel 文件
C# CLR, not able to connect to excel file
我正在尝试从 CLR 过程连接到 xlsx 文件。
我 运行 一个简单的测试器(不是 clr,只是一个控制台应用程序)中的代码并且它可以工作。
但是当我尝试 运行 (相同的代码)时,在 SQL serve management 2008 中,出现错误:"Unspecified error".
这是 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
我无法正常工作,求助。
是的,创建 OleDbConnection 时出现问题。在扩展属性的值周围添加引号。
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES';")
我正在尝试从 CLR 过程连接到 xlsx 文件。 我 运行 一个简单的测试器(不是 clr,只是一个控制台应用程序)中的代码并且它可以工作。 但是当我尝试 运行 (相同的代码)时,在 SQL serve management 2008 中,出现错误:"Unspecified error".
这是 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
我无法正常工作,求助。
是的,创建 OleDbConnection 时出现问题。在扩展属性的值周围添加引号。
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES';")