Microsoft Visual Studio中SSIS脚本任务引用Newtonsoft.Json.dll失效求助
SSIS脚本任务集成Newtonsoft.Json.dll的解决方法
你遇到的问题是SSIS脚本任务的引用机制和普通C#项目不同,直接放在项目根目录的DLL无法被SSIS运行时识别,以下是几种可行的解决方式:
方法一:在VSTA中添加引用并设置复制本地
- 打开SSIS脚本任务编辑界面,点击「编辑脚本」进入Visual Studio for Applications(VSTA)环境。
- 在VSTA的解决方案资源管理器里,右键点击「引用」→「添加引用」。
- 点击「浏览」,定位到项目根目录下的
Newtonsoft.Json.dll并添加。 - 选中刚添加的Newtonsoft.Json引用,在右侧属性窗口将「复制本地」设置为True。
- 保存脚本并关闭VSTA,重新运行SSIS包。
方法二:将DLL部署到全局程序集缓存(GAC)
- 确保
Newtonsoft.Json.dll是强签名版本(若未签名,需先对DLL进行强签名处理)。 - 以管理员身份打开命令提示符,执行以下命令(替换为你的DLL实际路径):
gacutil /i "C:\你的项目路径\Newtonsoft.Json.dll" - 完成后重启SSIS相关服务,再运行包即可。
方法三:通过变量指定DLL路径并动态加载
- 在SSIS包中创建一个字符串类型变量(比如命名为
DllFullPath),变量值设为Newtonsoft.Json.dll的完整绝对路径。 - 在脚本任务的「脚本」选项卡中,将
DllFullPath添加到「读取变量」列表。 - 编辑脚本,在
Main方法开头添加动态加载程序集的代码:string dllPath = Dts.Variables["DllFullPath"].Value.ToString(); System.Reflection.Assembly.LoadFrom(dllPath); - 保存脚本后重新运行包。
额外注意事项
- 确保使用的Newtonsoft.Json版本与SSIS运行环境的.NET框架版本兼容(例如SSIS 2017及以上通常基于.NET 4.x,需选择对应兼容的Newtonsoft.Json版本)。
- 你的代码中直接拼接SQL语句存在SQL注入风险,建议改用参数化查询优化:
string insertStatement = "INSERT INTO JSON_DATA(API, [Description], Auth, HTTPS, Cors, Link, Category) VALUES(@API, @Description, @Auth, @HTTPS, @Cors, @Link, @Category)"; SqlCommand command = new SqlCommand(insertStatement, connection); command.Parameters.AddWithValue("@API", entry.API); command.Parameters.AddWithValue("@Description", entry.Description); command.Parameters.AddWithValue("@Auth", entry.Auth); command.Parameters.AddWithValue("@HTTPS", entry.HTTPS); command.Parameters.AddWithValue("@Cors", entry.Cors); command.Parameters.AddWithValue("@Link", entry.Link); command.Parameters.AddWithValue("@Category", entry.Category);
内容的提问来源于stack exchange,提问作者vuilthe
相关产品推荐
相关产品推荐

