user2564223
user2564223

Reputation: 451

Checking if database contains table

I created the coding for a program to check if table exists in database. But it keeps underlining restrictions in the coding. Error 23 Value of type 'String' cannot be converted to '1-dimensional array of String' Can you please tell me what i did wrong and check if the rest of my coding correct.

Here is the coding:

    Dim cn As New SqlClient.SqlConnection(SQL_Loader("", My.Settings.SQL_Win_Auth, _
         My.Settings.SQL_Username, My.Settings.SQL_Password, My.Settings.SQL_Server_Name, _
         My.Settings.SQL_DB_Name))
    Dim Cmd As New SqlClient.SqlCommand
    Dim Reader As SqlClient.SqlDataReader

    Cmd.Connection = cn

    cn.Open()

    Dim restrictions As String
    restrictions = "Pastel_Companies"
    Dim dbTbl As DataTable = cn.GetSchema("Pastel_Companies", restrictions)

    If dbTbl.Rows.Count = 0 Then
        MessageBox.Show("Table Does Not Exist")
    Else
        MessageBox.Show("Table exists")
    End If

Thank you for any help given

Upvotes: 1

Views: 225

Answers (2)

Steve
Steve

Reputation: 216293

The correct syntax to call GetSchema is the following

Dim restrictions As String() = new String() {Nothing, Nothing, "Pastel_Companies"}
Dim dbTbl As DataTable = cn.GetSchema("TABLES", restrictions)

The first parameter is the collection that you want to check for the existence of the object (in your case you want to check the TABLES collection)
The second parameter contains an array of restrictions. This array changes following the collection that you want to search. For the TABLES collection you should apply three restrictions database, owner and tablename.
The restrictions should appear in the exact order expected, and if you haven't a value to specify you pass a null value (Nothing in VB)

Upvotes: 1

kwwallpe
kwwallpe

Reputation: 129

The offers a pretty good clue; you're passing a string as the second argument to GetSchema instead of a one-dimensional array of strings.

Try this:

Dim restrictions() as string = { Nothing, Nothing, "Pastel_Companies" }
Dim dbTbl As DataTable = cn.GetSchema("Tables", restrictions)

Upvotes: 0

Related Questions