creating databases programmatically using ADO.NET and VB.NET

code hunter

Member
Joined
Sep 7, 2006
Messages
7
Programming Experience
Beginner
Hi
wave.gif

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.gif

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.gif


here is the code:

VB.NET:
Imports System.Data.SqlClient
-----------------------------
VB.NET:
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/defaul...-US;Q305079#top

Thanks
 
Last edited by a moderator:
The most important line there is this one....

VB.NET:
Imports System.Data.SqlClient

That statement must go above everything else in your class..

VB.NET:
Imports System.Data.SqlClient
 
Public Sub new
end sub
 
...
..
 
Back
Top