SSIS脚本任务(VB):Newtonsoft.Json.Linq引用及C#转VB求助
在SSIS VB脚本任务中引用Newtonsoft.Json的解决方案
没问题,我帮你把那段C#代码转换成适配SSIS脚本任务的VB.NET版本,完美解决没法用NuGet引用Newtonsoft.Json的问题:
VB.NET代码实现
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> Partial Public Class ScriptMain Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase Shared Sub New() AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve End Sub Private Shared Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly ' 检查是否是Newtonsoft.Json的程序集请求 If args.Name.StartsWith("Newtonsoft.Json,", StringComparison.OrdinalIgnoreCase) Then ' 替换成你存放Newtonsoft.Json.dll的实际路径 Dim dllPath As String = System.IO.Path.Combine("C:\temp\", "Newtonsoft.Json.dll") Return System.Reflection.Assembly.LoadFile(dllPath) End If Return Nothing End Function ' 在这里写你的脚本任务逻辑,比如使用JObject/JArray等 Public Sub Main() ' 示例:解析JSON Dim jsonStr As String = "{""Name"":""Test"",""Value"":123}" Dim jObj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonStr) MsgBox(jObj("Name").ToString()) Dts.TaskResult = ScriptResults.Success End Sub End Class
关键注意事项
- DLL路径:确保
Newtonsoft.Json.dll(要选和.NET Framework 4.6兼容的版本)放在你指定的路径下,比如示例里的C:\temp\,也可以改成SSIS包能访问的网络路径或本地路径 - 编写时的提示:编写代码时Visual Studio可能会对
Newtonsoft.Json.Linq的引用显示红色波浪线,这是正常的——因为编译时没有直接引用,但运行时会通过AssemblyResolve事件动态加载DLL,执行时不会报错 - 程序集名称判断:
args.Name.StartsWith("Newtonsoft.Json,")是为了精准匹配Newtonsoft.Json的程序集请求,避免误加载其他DLL
内容的提问来源于stack exchange,提问作者user2587763
相关产品推荐
相关产品推荐

