How to create a table in Sql server from a datareader.GetSchemaTable DataTable

Can anyone help me with VB.NET code to create a table in Sql server from a datareader.GetSchemaTable DataTable?

I'm having trouble with comparing the datatype column and converting it.

I need a way to convert dBASE IV tables to SQL Server tables on the fly.
LVL 1
strauchaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vb_jonasCommented:
Hi!

Could you tell us more about your datatype problems? Are the GetSchemaTable not getting the correct type (DataType should be mapped to the .NET Framework type of the column.)

Are you using T-SQL or SMO to create the datatable in SQL Server?
0
vb_jonasCommented:
Here's code I wrote to test creating a datatable from datareader.GetSchemaTable:
 Sub CreateDb(ByVal strTableName As String, ByVal dtSchema As DataTable)
 
  ' dtSchema is a schema from 
  ' dtSchema=somekindofdatareader.GetSchemaTable()
 
  ' You will need references to 
  ' Microsoft.SqlServer.ConnectionInfo
  ' Microsoft.SqlServer.Smo()
  ' Microsoft.SqlServer.SmoEnum()
  ' Microsoft.SqlServer.SqlEnum()
 
  Dim server As New Server(".\SQLEXPRESS") ' local pc SQLEXPRESS
  Dim db As Database = server.Databases("TestDataBase")
 
  ' Create new table
  Dim newTable As New Table(db, strTableName)
 
  Dim rw As DataRow
 
  Dim smoColumn As Column
  Dim nColumnSize As Integer
  Dim nColumnNumericPrecision As Decimal
  Dim nColumnNumericScale As Decimal
  Dim strIndexColumnName As String = ""
 
  For Each rw In dtSchema.Rows
   smoColumn = New Column(newTable, rw("ColumnName"))
   Select Case rw("DataType").ToString
    '
    ' Handle datatype conversion - add the types you need
    '
    Case "System.Int32"
     smoColumn.DataType = DataType.Int
    Case "System.String"
     nColumnSize = rw("ColumnSize")
     If nColumnSize = -1 Then
      smoColumn.DataType = DataType.VarCharMax
     Else
      smoColumn.DataType = DataType.VarChar(nColumnSize)
     End If
    Case "System.Decimal"
     nColumnNumericPrecision = rw("NumericPrecision")
     nColumnNumericScale = rw("NumericScale")
     smoColumn.DataType = DataType.Decimal(nColumnNumericScale, nColumnNumericPrecision)
    Case Else
     Err.Raise(555, , "Handle this type please")
   End Select
   smoColumn.Nullable = rw("AllowDBNull")
   
   ' Handle id-column
   If rw("IsAutoIncrement") And rw("IsUnique") Then
    strIndexColumnName = rw("ColumnName")
    smoColumn.Identity = True
    smoColumn.IdentitySeed = 1
    smoColumn.IdentityIncrement = 1
   End If
   newTable.Columns.Add(smoColumn)
  Next
  If strIndexColumnName <> "" Then
   ' Create a PK Index
   Dim index As New Index(newTable, "PK_TestTable")
   index.IndexKeyType = IndexKeyType.DriPrimaryKey
   index.IndexedColumns.Add(New IndexedColumn(index, strIndexColumnName))
   newTable.Indexes.Add(index)
  End If
 
  ' Create the table in the database
  newTable.Create()
 
 
 End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
strauchaAuthor Commented:
I should have stated that I was using VS 2003, however your post still helped me.

My original problem was with the following Line:
Select Case rw("DataType").ToString

The .ToString doesn't appear as an option for the datarow("ColumnName") in the tip that pops up, only .getType, however as your code showed it works anyway.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.