我下面的脚本使用引用\Newtonsoft.Json.dll,它位于我的项目的根目录。
当我从控制流启动脚本时,出现一个错误,因为我的引用没有被考虑在内。
如何集成此引用,以便在从控制流选项卡启动脚本时也使用它?
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.Collections.Generic;
using System.Data.SqlClient;
#endregion
namespace ST_0651a7db5fbe4732a5944a76a31f37a4
{
public class Entry
{
public string API { get; set; }
public string Description { get; set; }
public string Auth { get; set; }
public bool HTTPS { get; set; }
public string Cors { get; set; }
public string Link { get; set; }
public string Category { get; set; }
}
public class Root
{
public int Count { get; set; }
public List<Entry> Entries { get; set; }
}
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var json = "";
using (WebClient wc = new WebClient())
{
json = wc.DownloadString("https://api.publicapis.org/entries");
}
Root root = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json);
foreach (Entry entry in root.Entries)
{
//Debug.WriteLine("API: " + entry.API);
//Debug.WriteLine("Description: " + entry.Description);
//Debug.WriteLine("Auth: " + entry.Auth);
//Debug.WriteLine("HTTPS: " + entry.HTTPS);
//Debug.WriteLine("Cors: " + entry.Cors);
//Debug.WriteLine("Link: " + entry.Link);
//Debug.WriteLine("Category: " + entry.Category);
//Debug.WriteLine("");
string connectionString = @"Data Source=*******;Initial Catalog=******;User ID=*******;Password=********;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the connection
connection.Open();
// Create the insert statement
string insertStatement = "INSERT INTO JSON_DATA(API, [Description], Auth, HTTPS, Cors, Link, Category) " +
"VALUES('" + entry.API.Replace("'", "''") + "', '" + entry.Description.Replace("'", "''") + "', '" + entry.Auth + "', '" + entry.HTTPS + "', '" + entry.Cors + "', '" + entry.Link + "', '" + entry.Category + "')";
// Create a SqlCommand object with the insert statement and the SqlConnection object
SqlCommand command = new SqlCommand(insertStatement, connection);
// Execute the insert statement
int rowsAffected = command.ExecuteNonQuery();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
字符串
但是,如果我用相同的代码创建一个C#项目并直接运行它,一切都很好。
1条答案
按热度按时间368yc8dk1#
引用的程序集必须安装在GAC中。
https://learn.microsoft.com/en-us/sql/integration-services/extending-packages-scripting/referencing-other-assemblies-in-scripting-solutions?view=sql-server-ver16
PS如果您计划在服务器上部署和运行SSIS项目,那么您还需要在目标服务器上将相同的程序集安装到GAC