Ciao

Ho la necessitą di eseguire tramite un SqlCommand una stringa sql.
Basandomi sui suggerimenti di un utente ho cercato di adattare il codice che ho utilizzato per eseguire una StoredProcedure.
Ma non mi funziona.

Ecco il codice:
codice:
Private Sub sCampi(ByRef strFile As String)
  Dim strSql As String = ""
  Dim indTab As Integer = 0
  Dim numRow As Integer = 0
  '
  Dim sNomeCampo As String = "", sTipoDato As String = ""
  Dim lLenCampo As Long = 0, sDefault As String = ""
  Dim bNull As Integer = 0, sCollation As String = "", 
  Dim sConstraints As String = ""
  '
  Dim strTit As New StringBuilder
  Dim fileSb As New StringBuilder(strFile)
  '
  Dim conn As New SqlConnection("DATA SOURCE=" & strServer & ";" _
      INITIAL CATALOG=" & strDataBase & ";UID='sa';PWD=''")
  '   
  Dim cmd As New SqlCommand("", conn)
  cmd.CommandType = CommandType.Text
  Dim da As New SqlDataAdapter(cmd)
  '
  '   array contentente l'elenco delle tabelle
  For indTab = 0 To UBound(ElencoTabelle) - 1

      ' Estrae l'elenco dei campi delle tabelle presenti nel database
      strSql = "SELECT DISTINCT TOP 100 PERCENT " _
       & "INFORMATION_SCHEMA.COLUMNS.table_name, " _
       & "INFORMATION_SCHEMA.COLUMNS.column_name, " _
       & "INFORMATION_SCHEMA.COLUMNS.column_default, " _
       & "INFORMATION_SCHEMA.COLUMNS.is_nullable, " _
       & "INFORMATION_SCHEMA.COLUMNS.data_type, " _
       & "ISNULL(INFORMATION_SCHEMA.COLUMNS.Collation_Name,_
          'SQL_Latin1_General_CP1_CI_AS') AS Collation, " _
       & "dbo.syscolumns.length, dbo.syscolumns.prec " _
       & "FROM " _
       & "INFORMATION_SCHEMA.COLUMNS  LEFT OUTER JOIN dbo.syscolumns " _
       & "ON INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = dbo.syscolumns.name " _
       & "WHERE " _
       & "TABLE_NAME = '" & ElencoTabelle(indTab).ToString() & "' " _
       & "ORDER BY INFORMATION_SCHEMA.COLUMNS.column_name, " _
       & "INFORMATION_SCHEMA.COLUMNS.column_default"

       cmd.CommandText = strSql
       cmd.ExecuteReader()

       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

             sNomeCampo = ""
             sTipoDato = ""
             lLenCampo = 0
             bNull = 0
             sDefault = ""
             sCollation = ""
             sConstraints = ""

             sNomeCampo = row("COLUMN_NAME").ToString()
             sTipoDato = row("DATA_TYPE").ToString()
             If sTipoDato = "varchar" OrElse _
                sTipoDato = "nvarchar" Then
                        lLenCampo = row("prec").ToString()
             Else
                        lLenCampo = row("length").ToString()
             End If
             If row("IS_NULLABLE").ToString() = "yes" Then
                bNull = 1
             Else
                bNull = 0
             End If
             'default value č null
             If IsDBNull(row("COLUMN_DEFAULT").ToString()) Then
                sDefault = ""
             Else
                sDefault = Replace(CStr(row("COLUMN_DEFAULT").ToString()), _
                           "'", "''")
             End If

             sCollation = "SQL_Latin1_General_CP1_CI_AS"
             sConstraints = ""

             'Scrivo il testo contenuto nelle 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}, ", sNomeCampo)
             fileSb.AppendFormat("{0}, ", sTipoDato)
             fileSb.AppendFormat("{0}, ", lLenCampo)
             fileSb.AppendFormat("{0}, ", bNull)
             fileSb.AppendFormat("{0}, ", sDefault)
             fileSb.AppendFormat("{0}, ", sCollation)
             fileSb.AppendFormat("{0}, {1}", sConstraints, Environment.NewLine)


             '   Inserisce i dati nella tabella tbCampiTabelle
             strSql = "INSERT INTO tbCampiTabella(" _
               & "IDTabella, NomeCampo, _
                  TipoDato, LunghezzaCampo, _
                  AmmetteNull, ValoreDefault, _
                  CollationCampo, ConstraintsCampo) " _
               & "VALUES(" _
               & "0, '[" & sNomeCampo & "]', " _
               & "'" & sTipoDato & "', " & lLenCampo & ", " _
               & " & bNull & ", '" & sDefault & "', " _
               & "'" & sCollation & "', '" & sConstraints & "')"

               adoConnACC.Execute(strSql)

          Next
          '
       End If
       dt.Dispose()

  Next

  da.Dispose()
  cmd.Dispose()
  conn.Dispose()
  strFile = fileSb.ToString()

  MsgBox("Importazione terminata con successo", _
  MsgBoxStyle.Information + MsgBoxStyle.OKOnly, Tit_MsgBox)

End Sub
Va in errore sulla riga in rosso, qual č l'execute corretta per questo tipo di sqlcommand?
Il testo dell'errore č questo:
5: ExecuteReader requires an open and available Connection. The connection's current state is closed.


Grazie a tutti
lady