Results 1 to 4 of 4

Thread: check if SQL table exists

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    165

    check if SQL table exists

    Hello,

    I am implemeting a window application in vb.net using Visual Studio 2005, how can i check if SQL table is created in SQL server from my application cuz i want to disable that button if the sql table is created and enable that button if that table doesnt exist in order to create it.


    Thank you

    Hiba

  2. #2
    Hyperactive Member koolprasad2003's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    443

    Arrow Re: check if SQL table exists

    MCP, MCTS, Microsoft MVP [Asp.Net/IIS]

    For more .NET development tips visit .NET Tips

    If the post is useful then please Rate it

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    165

    Re: check if SQL table exists

    Hello,

    How can I use this statment :

    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))


    in Vb.net code?

    Thanx

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: check if SQL table exists

    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Data.SqlClient
    5.  
    6. Public Class MyForm
    7.     Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim conn As New SqlConnection()  'Creates a new connection object
    9.         Dim cmd As New SqlCommand()      'Creates a new command object
    10.  
    11.         Dim exists As Byte = 0           'Creates a byte variable that will store the cmd return value
    12.  
    13.         conn.ConnectionString = "Data Source=MyServer; " & _
    14.                                 "Initial Catalog=MyDatabase; " & _
    15.                                 "User Id=MyUserId; " & _
    16.                                 "Password=MyPassword"
    17.  
    18.         cmd.CommandText = "SELECT COUNT(*) " & _
    19.                            "FROM sys.objects " & _
    20.                            "WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') " & _
    21.                            "AND type in (N'U')"
    22.  
    23.         Try
    24.             conn.Open()                             'Opens the connection
    25.             cmd.Connection = conn                   'Instructs the cmd object to use conn as its connection when executing
    26.             exists = CByte(cmd.ExecuteScalar())     'Use ExecuteScalar to return a single value, the count, and assign it to the exists variable
    27.             conn.Close()
    28.         Catch ex As SqlException
    29.             MessageBox.Show(ex.Message)             'Catch any SqlException and display it in a MessageBox
    30.         Catch ex As Exception
    31.             MessageBox.Show(ex.Message)             'Catch any ApplicationException and display it in a MessageBox
    32.         End Try
    33.  
    34.         MyButton.Enabled = CBool(exists)            'Convert the exists variable to a boolean to determine whether the button should be enabled.
    35.  
    36.         'Note that by assigning the exists variable a value of 0 earlier, if there is an error in connecting to the database, the value will
    37.         'remain 0, and the button will be disabled by default.  
    38.  
    39.     End Sub
    40. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width