Results 1 to 7 of 7

Thread: Create a Database Programmatically and

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    246

    Create a Database Programmatically and

    I am learning VB2019 Student edition and it is the very first time that I interact with the VB2019 code. The project to do is to Create a Database and a table programmatically separately. One program for creating a database and create a table. So, I just started with the database, and I got this code from a tutorial over the internet and I just compiled it but, I got this error BC30002 Type 'sqlconnection' is not defined. I was looking for some references on the internet and I did not find this error. Can somebody with visual basic knowledge help me, please?


    Public Class Form1

    Private Sub btnCreateDatabase_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
    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
    mannyso

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,891

    Re: Create a Database Programmatically and

    Put this on top of your code:

    Imports System.Data.SqlClient
    .

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    246

    Re: Create a Database Programmatically and

    Quote Originally Posted by sapator View Post
    Put this on top of your code:

    Imports System.Data.SqlClient
    Yes, I did I put it right on top and now I got all these messages, see this, please?
    Name:  1000000.jpg
Views: 41
Size:  232.1 KB
    mannyso

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,891

    Re: Create a Database Programmatically and

    Please check your connection string:
    https://www.connectionstrings.com/sql-server/

    At least this: Server=(local)\netsdk;
    I believe should be this:Server=(local); Database=netsdk;
    .

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,579

    Re: Create a Database Programmatically and

    For future reference, please don't post only pictures of error messages. You can provide a picture as well, if it adds some value, but you should ALWAYS provide the error message as text. If we want to search the web for that error message, we can't copy and paste from a picture.

    Anyway, that means that there's an issue with your connection string. The error message is basically telling you what the issue is, where it happened and what to do about it. Either you have specified a database or instance that doesn't exist or is inaccessible or you have provided credentials that don't provide the required permissions.

    By the way, that's not "all these messages". It's one message. The text provides the stack trace of the exception, which is the list of methods that were executing at the time. If you look right at the bottom you can see your method, i.e. btnCreateDatabase_Click. The next frame is SqlConnection.Open, which your method calls. The remaining frames are all the methods that get called internally from Open until the exception is thrown in the SqlInternalConnectionTds constructor.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,579

    Re: Create a Database Programmatically and

    Quote Originally Posted by sapator View Post
    Please check your connection string:
    https://www.connectionstrings.com/sql-server/

    At least this: Server=(local)\netsdk;
    I believe should be this:Server=(local); Database=netsdk;
    The database is master, which is also the default if a database isn't specified. You generally wouldn't connect to an existing database to create a new database.

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,891

    Re: Create a Database Programmatically and

    Aha. i missed the database string that it was at the end.
    So first try to check you configuration on TCP:
    Go to Start->Programs->Microsoft SQL Server 2005/2008/2012 ->Configuration Tools and select the SQL Server Configuration Manager:
    Under the SQL Server Network Configuration select Protocols for <your server name>:
    Make sure that TCP/IP protocol is enabled (lot of info from here: https://knowledgebase.apexsql.com/co...apexsql-tools/)

    and then read this: https://support.microsoft.com/en-us/...2-22e1694bb889
    .

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