How do I convert a SQL IF statement to a string?
Posted on 2014-08-02
I run this query in SQL and it works great. It is to add a column if it doesn't exist in the existing table.
IF NOT EXISTS (SELECT *
WHERE TABLE_NAME = 'tblInventory' AND COLUMN_NAME = 'Image')
ALTER TABLE tblInventory ADD image IMAGE
I want to add it to my application code and I can't seem to get it to work. Here is one of the MANY variations I have been trying:
Dim strTableAlter As String
Dim myConnT1 As SqlConnection = New SqlConnection("Server=" & machine & "\" & Inst & ";Trusted_Connection=True;")
strTableAlter = "USE [AddBook]" & _
"IF NOT EXISTS (SELECT *" & _
"FROM INFORMATION_SCHEMA.COLUMNS" & _
"WHERE [tblInventory] AND [IMAGE])" & _
"ALTER TABLE [tblInventory] ADD [image] [IMAGE]"
Dim myCommandT2 As SqlCommand = New SqlCommand(strTableAlter, myConnT1)
MsgBox("Your Household Inventory Table has been updated to the latest version.", MsgBoxStyle.OkOnly, "Table Upgrade")
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
Any help is appreciated.