Generating .NET POCO Classes for SQL Query Results: Listing 1

Using ADO.NET to retrieve the schema of a dynamic query.

Private Function GetQuerySchema(ByVal strconn As String, ByVal strSQL As String) As DataTable
  Dim sconQuery As New SqlConnection
  Dim scmdQuery As New SqlCommand
  Dim srdrQuery As SqlDataReader = Nothing
  Dim intRowsCount As Integer = 0
  Dim dtSchema As New Data.DataTable
    sconQuery.ConnectionString = strconn
    scmdQuery.Connection = sconQuery
    scmdQuery.CommandText = strSQL

    srdrQuery = scmdQuery.ExecuteReader(Data.CommandBehavior.SchemaOnly)
    dtSchema = srdrQuery.GetSchemaTable
  Catch ex As Exception
    Err.Raise(-1000, , "Error = '" & ex.Message & " ': sql = " & strSQL)
    If Not IsNothing(srdrQuery) Then
      If Not srdrQuery.IsClosed Then srdrQuery.Close()
    End If
  End Try
  Return dtSchema
End Function

About the Author

Joe Kunk is a Microsoft MVP in Visual Basic, three-time president of the Greater Lansing User Group for .NET, and developer for Dart Container Corporation of Mason, Michigan. He's been developing software for over 30 years and has worked in the education, government, financial and manufacturing industries. Kunk's co-authored the book "Professional DevExpress ASP.NET Controls" (Wrox Programmer to Programmer, 2009). He can be reached via email at [email protected].

comments powered by Disqus


  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

Upcoming Events