我有一个常见的SSIS C#脚本任务块,我们在公司使用。它只是一个脚本块,但内部的脚本始终是相同的。我们在很多SSIS包中使用它。总是必须从另一个项目中复制块或将脚本从某个地方复制到新的脚本块中,这是一种痛苦。
有什么方法可以将带有预先编写的脚本的脚本块的副本放入SSIS工具箱中,这样我就可以将它拖放到我们的项目中了吗?
修改
好的,所以我已经开始根据下面给出的建议编写自定义控件。除了连接到我的SQL Server数据库之外,我已经完成了所有工作。使用以下代码:
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())
{
我收到此错误消息:
[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]. ".
我做错了什么?
另一个编辑:
如果我将代码更改为:
string myConnectionStr = _selectedConnectionManagerSource.ConnectionString;
SqlConnection SettingsConnection = new SqlConnection(myConnectionStr);
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;
我收到此错误消息:
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.
如果我取出连接字符串的'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;
我收到此错误消息:
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.
这最后一个块正是我在Script Task块中使用的代码,它运行正常。我只是不确定两者之间的区别......:/
最终编辑:
所有这些错误消息都是因为我的测试包设置为运行64位而不是32位。我将程序包更改为运行32位,并开始使用ADO.NET连接。我的最终代码如下所示:
string myConnectionStr = _selectedConnectionManagerSource.ConnectionString;
SqlConnection SettingsConnection = new SqlConnection(myConnectionStr);
SqlCommand sp_GetAllValues = new SqlCommand();
sp_GetAllValues.Connection = SettingsConnection;
sp_GetAllValues.CommandType = CommandType.StoredProcedure;
只要它使用ADO.NET数据源,它就能很好地工作。即使使用OleDbConnection等,它仍然无法使用OleDb数据源。不确定原因。但是,既然它正在使用ADO.NET,我真的不在乎解决那个问题。
既然基本功能正常,我就要编写UI ......:)
答案 0 :(得分:2)
唯一的方法是创建它的自定义任务:http://microsoft-ssis.blogspot.com/2013/06/create-your-own-custom-task.html
另一种方法是在脚本任务中创建自定义程序集和引用:http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html
答案 1 :(得分:1)
所以我认为你遇到了涉及你的连接提供者的障碍:
所以在你的代码中你应该使用OleDbConnection而不是SqlConnection(以及OleDbCommand,OleDbDataReader等)
对于您的硬编码连接字符串,您的服务器实例名称是否恰好有斜杠?这让我失去了一次,你必须逃避C#字符串中的斜杠,所以如果你的连接字符串是
Data Source=SERVER\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;
您的C#代码应如下所示:
string ConnStr = "Data Source=SERVER\\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;";
或(我的偏好)
string ConnStr = @"Data Source=SERVER\INSTANCE;Initial Catalog=DatabaseName;Integrated Security=SSPI;";