Reputation: 35
Somehow I can't create a Table in the database by running the code. However, SQL inquiry works...
I was thinking it's my connection string. I tried both with my.settings and as plain text however I get no errors and it still doesn't work.
Dim conStr As String = My.Settings.SDB
Dim objCon As New SqlConnection(conStr)
Dim obj As SqlCommand
Dim strSQL As String
Try
objCon = New SqlConnection(conStr)
objCon.Open()
obj = objCon.CreateCommand()
strSQL = "CREATE TABLE Names (Id int NOT NULL PRIMARY KEY, LastName VARCHAR(30), FirstName VARCHAR (30))"
'Execute
obj.CommandText = strSQL
obj.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
objCon.Close()
objCon = Nothing
No errors, works in actual SQL command..
Upvotes: 0
Views: 90
Reputation: 35
OK, guys, I figured it out.... There is nothing wrong with the code. Its a terrible VB.net emulation - it creates a 2nd Database in DEBUG folder and applies changes to it instead of the original location. You will have to add this extra Database to your server explorer in order to see the changes. Credit - "https://forums.asp.net/t/1378272.aspx"
Upvotes: 1
Reputation: 149
Dim conStr As String = My.Settings.SDB
Dim objCon As New SqlConnection(conStr)
Dim obj As SqlCommand
Dim strSQL As String
Try
objCon = New SqlConnection(conStr)
objCon.Open()
strSQL = "CREATE TABLE Names (Id int NOT NULL PRIMARY KEY, LastName
VARCHAR(30), FirstName VARCHAR (30))"
obj = objCon.CreateCommand(strSQL)
obj.CommandText = strSQL
obj.ExecuteNonQuery()
Exit Try
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
objCon.Close()
MessageBox.Show("Table Created Successfully","Anything")
End Try
Upvotes: 0