I'm trying to create my own Database and Tables in SQL Server 2005 using VB.NET 2005. I get an error when I try ExecuteNonQuery, which says that the connection is not initialized. I'm not sure if I'm doing any of this right.
Any help would be greatly appreciated!
Sheryl
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim obj As New SqlCommand
' am I supposed to leave the database blank here?
Dim conStr As String = "Server=DIMENSION9100;Database=;Trusted_Connection = yes"
Dim objCon As New SqlConnection(conStr)
Try
objCon.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
obj.CommandText = "CREATE DATABASE MyDataBase"
Try
' this is where I get the error
obj.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
' is this syntax correct?
' creat Table People
obj.CommandText = "CREATE TABLE MyDataBase, People " & _
"(" & _
"Id int NOT NULL PRIMARY KEY " & _
"LastName VARCHAR(30) " & _
"FirstName VARCHAR(20) " & _
"Address VARCHAR(50) " & _
") IN MyDatabase"
obj.ExecuteNonQuery()
End Sub