creating database programmatically using ADO.NET and VB.NET
Hi :wave:
i am using VB.NET 2005 i tried to creat data base file by using this code but i got 3 errors as the follow :eek2:
1 'Imports' statements must precede any declarations.
2 Type 'SqlConnection' is not defined.
3 Type 'SqlCommand' is not defined.
i need help can any perfect programmer tell me what is the wrong with this way? :confused:
here is the code:
Imports System.Data.SqlClient
-----------------------------
Dim str As String
Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
"uid=sa;pwd=;database=master")
str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
"(NAME = MyDatabase_Data, " & _
" FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
" SIZE = 2MB, " & _
" MAXSIZE = 10MB, " & _
" FILEGROWTH = 10%) " & _
" LOG ON " & _
"(NAME = MyDatabase_Log, " & _
" FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
" SIZE = 1MB, " & _
" MAXSIZE = 5MB, " & _
" FILEGROWTH = 10%) "
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
Try
myConn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Database is created successfully", _
"MyProgram", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try
-----------------------------
i got this code from MS support site here is the link.
http://support.microsoft.com/default...US;Q305079#top
Thanks
Re: creating database programmatically using ADO.NET and VB.NET
Fix the first issue and you fix the others. You have to put the imports at the very top of the code file. The only lines that can precede the imports are comments and Option settings. Once you fix that the import will be recognised and the other errors will disappear.
Re: creating database programmatically using ADO.NET and VB.NET
Thank You For Help Buddy.
Re: creating database programmatically using ADO.NET and VB.NET
If your issue is resolved please use the Thread Tools menu to mark the thread as such.