Trouble creating SQL Server 2008 db in VB 2010 Express
Hi! I'm trying to create a test DB in SQL Server 2008, using Visual Basic 2010 Express. I'm failing each time--no message, just nothing shows up. Is it my connection string?
Private Sub createDB()
'Dim conStr as String = "Server=.;uid=sa;pwd=;database=MemoryDB"
'Dim conStr as String = "Server=.;database=MemoryDB;Trusted_Connection=True;")
'Dim conStr as String = "Server=.\SQLExpress;AttachDbFilename=C:\Memory\MemoryDB.mdf")
'Dim conStr As String = "Server=.\SQLExpress;" & _
'"AttachDbFilename=C:\MyFolder\" & _
'"MyDataFile.mdf;Database=dbname;Trusted_Connection=Yes;"
Dim conStr As String = "Server=.\SQLExpress;" & _
"AttachDbFilename=C:\MyFolder\" & _
"MyDataFile.mdf;Trusted_Connection=True;"
Dim str As String
Dim sCon As SqlConnection = New SqlConnection(conStr)
str = "CREATE DATABASE MemoryDB ON PRIMARY " & _
"(NAME = MemoryDB_Data, " & _
" FILENAME = 'C:\Memory\MemoryDB.mdf', " & _
" SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " & _
" LOG ON " & _
"(NAME = MemoryDB_Log, " & _
" FILENAME = 'C:\Memory\MemoryDBLog.ldf', " & _
" SIZE = 1MB, MAXSIZE = 5MB, FILEGROWTH = 10%) "
Dim myCommand As SqlCommand = New SqlCommand(str, sCon)
Try
sCon.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Database created successfully", _
"MyProgram", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (sCon.State = ConnectionState.Open) Then
sCon.Close()
End If
End Try
End Sub ' createDB