by Maxi Accotto
12. marzo 2011 09:59
El siguiente código de VB.net y utilizando SQL-DMO permite conectarnos a un servidor SQL Server 2000 y cambiar las cadenas de conexión de los paquetes a seguridad integrada.
Recuerden antes de utilizar el código de hacer referencias a las clases de DTS del SQL-DMO
Code Snippet
- Public Class Form1
- 'Declare theobjects
- Dim Opkg As DTS.Package = New DTS.Package()
- Dim PkgName As String
- Dim ServerName As String
- Dim ConnectionString As String
- Dim Con As Connections
- Dim NumOfCon As Integer
- Dim Str As String = ""
- Dim myConnection As SqlConnection
- Dim myCommand1 As SqlCommand
- Dim myCommand2 As SqlCommand
- Dim dr As SqlDataReader
-
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
- End Sub
-
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- 'Establish ODBC connections with SQL Server Instance
- myConnection = New SqlConnection("Server = servidor; uid = usuario; " &
- "pwd = password; database =msdb")
-
- 'Opening the connection
- myConnection.Open()
- 'Executing the command and assigning it to my connection
- myCommand1 = New SqlCommand("SELECT distinct(name) FROM sysdtspackages order by name", myConnection)
- 'Read from the datareader
- dr = myCommand1.ExecuteReader()
-
- While dr.Read()
- Try
- Opkg = New DTS.Package()
- 'Load DTS package from SQL Server
- Opkg.LoadFromSQLServer("servidor", "usuario", "password",
- DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "", "", "", dr(0).ToString())
-
- ' Get the loaded DTS package name
- PkgName = Opkg.Name()
- 'Get the number of connections within the loaded DTS package
- 'Get the connection string of each connections of the DTS package
-
- For NumOfCon = 1 To Opkg.Connections.Count
- Opkg.Connections.Item(NumOfCon).UseTrustedConnection = True
- Next
-
- Opkg.SaveToSQLServer("servidor", "usuario", "password",
- DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "", "", "", dr(0).ToString())
-
- Catch ex As Exception
- If Err.Number = -2147220454 Then
- Continue While
- Else
- MsgBox(Err.Description)
- End If
-
- End Try
- End While
-
- proceso.Value = proceso.Value + 1
- dr.Close()
- MsgBox("Se termino el proceso")
-
- End Sub
- End Class