Rieccomi
Ho modificato un po' il codice per quello che chiedevi, ora il command viene dichiarato soltanto una volta fuori dal ciclo ed internamente viene soltanto cambiato il valore del parametro.
Ho poi ottimizzato le concatenazioni delle stringhe utilizzando StringBuilder come è sempre consigliabile fare.
Se ci sono problemi o qualcosa che non ti è chiaro chiedimi pure.
codice:Private Sub sRelazioni(ByRef strFile As String) Dim strTit As New Text.StringBuilder Dim fileSb As New Text.StringBuilder(strFile) Dim indTab As Integer = 0 Dim numRow As Integer = 0 ' Dim conn As New SqlConnection("DATA SOURCE='(local)';INITIAL CATALOG='master';UID='sa';PWD=''") Dim cmd As New SqlCommand("sp_fkeys", conn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add("@pktable_name", SqlDbType.VarChar) Dim da As New SqlDataAdapter(cmd) ' ciclo l'array For indTab = 0 To ElencoTabelle.Length - 1 cmd.Parameters("@pktable_name").Value = ElencoTabelle(indTab) Dim dt As New DataTable da.Fill(dt) numRow = dt.Rows.Count If numRow > 0 Then strTit.Remove(0, strTit.Length) For Each row As DataRow In dt.Rows 'Ciclo le righe If strTit.ToString() <> ElencoTabelle(indTab) Then strTit.Append(" * ") strTit.Append(ElencoTabelle(indTab)) fileSb.AppendFormat("{0}{1}{0}", Environment.NewLine, strTit) End If fileSb.AppendFormat(" - {0}, ", row("FK_NAME").Value.ToString()) fileSb.AppendFormat("{0}, ", row("PKTABLE_NAME").Value.ToString()) fileSb.AppendFormat("{0}, ", row("FKTABLE_NAME").Value.ToString()) fileSb.AppendFormat("{0}, ", row("PKCOLUMN_NAME").Value.ToString()) fileSb.AppendFormat("{0}, {1}", row("FKCOLUMN_NAME").Value.ToString(), Environment.NewLine) Next End If dt.Dispose() Next da.Dispose() cmd.Dispose() conn.Dispose() strFile = fileSb.ToString() End Sub



Rispondi quotando