简体   繁体   English

是否可以将带有自定义脚本的SSIS脚本块添加到工具箱中?

[英]Can I add an SSIS script block with custom script into the toolbox?

I have a common SSIS C# script task block that we use in our company. 我在公司中使用了一个常见的SSIS C#脚本任务块。 It is only a single script block but the script inside is always the same. 它只是一个脚本块,但内部的脚本始终相同。 We use it in lots of our SSIS packages. 我们在许多SSIS软件包中都使用了它。 It is kind of a pain to always have to copy the block from another project or copy the script from somewhere into a new script block. 总是不得不从另一个项目复制该块或将脚本从某个地方复制到一个新的脚本块中,这是一种痛苦。

Is there any way I can put a copy of that script block with the prewritten script into the SSIS Toolbox so I can just drag-and-drop it into our projects? 有什么办法可以将带有预写脚本的脚本块的副本放入SSIS工具箱中,以便将其拖放到我们的项目中?

EDIT: 编辑:

OK, so I have started writing a custom control based on the advice given below. 好的,因此我已经开始根据以下建议编写自定义控件。 I've got everything working except for connecting to my SQL Server database. 除了连接到我的SQL Server数据库之外,我已经完成了所有工作。 Using the following code: 使用以下代码:

SqlConnection SettingsConnection = (SqlConnection)_selectedConnectionManagerSource.AcquireConnection(transaction); // Errors here
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;
sp_GetAllValues.CommandText = "Get_My_Data_For_Project";
sp_GetAllValues.Parameters.AddWithValue("@project_name", pkgname);
SettingsConnection.Open();
SqlDataReader SettingsReader = sp_GetAllValues.ExecuteReader();
while (SettingsReader.Read())
{

I get this error message: 我收到此错误消息:

[Connection manager "DevServer"] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005 Description: "Login timeout expired". An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005 Description: "A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.". An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005 Description: "Named Pipes Provider: Could not open a connection to SQL Server [53]. ".

What am I doing wrong? 我究竟做错了什么?

ANOTHER EDIT: 另一个编辑:

If I change the code to: 如果我将代码更改为:

string myConnectionStr = _selectedConnectionManagerSource.ConnectionString;
SqlConnection SettingsConnection = new SqlConnection(myConnectionStr);
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;

I get this error message: 我收到此错误消息:

Error: The Execute method on the task returned error code 0x80070057 (Keyword not supported: 'provider'.). The Execute method must succeed, and indicate the result using an "out" parameter.

If I take out the 'provider' section of the connection string (hard code the string): 如果我取出连接字符串的“ provider”部分(硬编码该字符串):

string myConnectionStr;
myConnectionStr = "Data Source=mydatabase;Initial Catalog=mycatalog;Integrated Security=SSPI;";
SqlConnection SettingsConnection = new SqlConnection(myConnectionStr);
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;

I get this error message: 我收到此错误消息:

Error: The Execute method on the task returned error code 0x80131904 (A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)). The Execute method must succeed, and indicate the result using an "out" parameter.

This last block is exactly the code I was using in my Script Task block and it is working fine. 最后一块恰好是我在“脚本任务”块中使用的代码,并且工作正常。 I'm just not sure what the difference between the two... :/ 我只是不确定两者之间的区别...:/

FINAL EDIT: 最终编辑:

All of these error messages were because my test package was set to run 64-bit instead of 32-bit. 所有这些错误消息是因为我的测试包设置为运行64位而不是32位。 I changed the package to run 32-bit and it started working with an ADO.NET connection. 我将程序包更改为运行32位,并且它开始与ADO.NET连接一起使用。 My final code looks like this: 我的最终代码如下:

string myConnectionStr = _selectedConnectionManagerSource.ConnectionString;
SqlConnection SettingsConnection = new SqlConnection(myConnectionStr);
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;

and it works great as long as it is using an ADO.NET data source. 只要使用ADO.NET数据源,它就可以很好地工作。 It still would not work with an OleDb data source even using OleDbConnection, etc. Not sure why. 即使使用OleDbConnection等,它仍然不能与OleDb数据源一起使用。不知道为什么。 But, now that it is working with ADO.NET, I really don't care to solve that one. 但是,既然它可以与ADO.NET一起使用,我真的不在乎解决那个问题。

Now that the base functionality is working fine, I'm off to write the UI... :) 既然基本功能运行良好,我就可以编写UI ... :)

The only way is to create custom task of it: http://microsoft-ssis.blogspot.com/2013/06/create-your-own-custom-task.html 唯一的方法是创建它的自定义任务: http : //microsoft-ssis.blogspot.com/2013/06/create-your-own-custom-task.html 在此处输入图片说明在此处输入图片说明

An alternative is to create a custom assembly and reference that in your Script Task: http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html 一种替代方法是在脚本任务中创建一个自定义程序集并对其进行引用: http : //microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html

So I think you ran into a snag involving your connection's provider: 因此,我认为您遇到了涉及连接的提供程序的障碍:

  1. You can't include the provider keyword in a SqlConnection connection string. 您不能在SqlConnection连接字符串中包含provider关键字。 All of the connections in SSIS are OLE DB connections which include the provider keyword in its connection string. SSIS中的所有连接都是OLE DB连接,其连接字符串中包含provider关键字。

So in your code you should be using an OleDbConnection instead of a SqlConnection (and OleDbCommand, OleDbDataReader, etc.) 因此,在您的代码中,您应该使用OleDbConnection而不是SqlConnection(以及OleDbCommand,OleDbDataReader等)。

For your hardcoded connection string, does your server instance name happen to have a slash in it, by any chance? 对于您的硬编码连接字符串,您的服务器实例名称是否碰巧带有斜线? This tripped me up once, you have to escape slashes in C# strings, so if your connection string is 这使我绊倒了一次,您必须在C#字符串中转义斜线,所以如果您的连接字符串是

Data Source=SERVER\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;

Your C# code should look like so: 您的C#代码应如下所示:

string ConnStr = "Data Source=SERVER\\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;";

or (my preference) 或(我的偏好)

string ConnStr = @"Data Source=SERVER\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM