Results 1 to 4 of 4

Thread: creating database programmatically using ADO.NET and VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    creating database programmatically using ADO.NET and VB.NET

    Hi
    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
    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?

    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
    Last edited by code hunter; Sep 11th, 2006 at 07:17 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    16

    Re: creating database programmatically using ADO.NET and VB.NET

    Thank You For Help Buddy.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width