Here is my code database and table I am so confuse

manny cash

Member
Joined
Oct 19, 2024
Messages
7
Programming Experience
Beginner
Asking for somebody who can help me, please : Thank you so much.

Imports System.Data.SqlClient

Public Class Form1
Private objCon As Object

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

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=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True; ")


str = "CREATE DATABASE visitas ON PRIMARY " &
"(NAME = MyDatabase_Data, " &
" FILENAME = 'C:\Project\visitas.mdf', " &
" SIZE = 2MB, " &
" MAXSIZE = 10MB, " &
" FILEGROWTH = 10%)" &
" LOG ON " &
"(NAME = MyDatabase_Log, " &
" FILENAME = 'C:\Project\visitasLog.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




'Create a table
Dim constr As String
Dim myconn As String = Nothing


' = "Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True; "
myConn = New SqlConnection(constr)
myConn.Open()
myCommand = objCon.CreateCommand()
Dim strSQL As String
strSQL = "CREATE TABLE Names (LastName VARCHAR(30), FirstName VARCHAR (30))"



' Execute
myCommand.CommandText = strSQL
Try
myCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

myConn.Close()
myConn = Nothing
End Sub
Private Function myconn() As String
Throw New NotImplementedException()
End Function
End Class
 
Help with what exactly? What are you trying to achieve? What happens that you don't expect and/or what do you expect that doesn't happen? Please provide a full and clear explanation of the problem.
 
Help with what exactly? What are you trying to achieve? What happens that you don't expect and/or what do you expect that doesn't happen? Please provide a full and clear explanation of the problem.

I am sorry yes I didn't because somebody need the computer at that time and I forgot to complete my message, Thank you once again.

Hi jmcihinney, yeha.

On the line number 6 it say "end sub" expected
on the line number 8 statement can not appear with a method body, end of statement assumed

on line number 11 it say Event 'Load" cannot be found

online number 16 it say Handle clause requires a whitevent variable defined in the containing type or one of his base type

on line number 60 it say variable 'constr' it used before it has been assigned a value.

there are the error
 
This worked for me, of course change the connection string and file locations as needed

VB.NET:
Imports System.Data.SqlClient

Public Class Form1
    Private objCon As Object

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnCreateDatabase_Click(sender As Object, e As EventArgs) Handles btnCreateDatabase.Click

        Dim myConn As SqlConnection = New SqlConnection("Server=(localdb)\MSSQLLocalDB;Database=master;Trusted_Connection=True;")
        Dim str As String

        str = "CREATE DATABASE visitas ON PRIMARY " &
        "(NAME = MyDatabase_Data, " &
        " FILENAME = 'F:\Temp\visitas.mdf', " &
        " SIZE = 2MB, " &
        " MAXSIZE = 10MB, " &
        " FILEGROWTH = 10%)" &
        " LOG ON " &
        "(NAME = MyDatabase_Log, " &
        " FILENAME = 'F:\Temp\visitasLog.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.Message)

            myConn.Close()
            Exit Sub
        End Try

        myConn.Close()
        myConn.ConnectionString = "Server=(localdb)\MSSQLLocalDB;Database=visitas;Trusted_Connection=True;"
        myConn.Open()

        'Create a table in the new database
        myCommand.CommandText = "CREATE TABLE People (LastName VARCHAR(30), FirstName VARCHAR (30))"
        Try
            myCommand.ExecuteNonQuery()
            MessageBox.Show("Table is created successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        myConn.Close()
        myConn = Nothing
    End Sub
End Class
 

Attachments

  • Screenshot 2024-11-02 124629.png
    Screenshot 2024-11-02 124629.png
    10.6 KB · Views: 8
This worked for me, of course change the connection string and file locations as needed

VB.NET:
Imports System.Data.SqlClient

Public Class Form1
    Private objCon As Object

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnCreateDatabase_Click(sender As Object, e As EventArgs) Handles btnCreateDatabase.Click

        Dim myConn As SqlConnection = New SqlConnection("Server=(localdb)\MSSQLLocalDB;Database=master;Trusted_Connection=True;")
        Dim str As String

        str = "CREATE DATABASE visitas ON PRIMARY " &
        "(NAME = MyDatabase_Data, " &
        " FILENAME = 'F:\Temp\visitas.mdf', " &
        " SIZE = 2MB, " &
        " MAXSIZE = 10MB, " &
        " FILEGROWTH = 10%)" &
        " LOG ON " &
        "(NAME = MyDatabase_Log, " &
        " FILENAME = 'F:\Temp\visitasLog.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.Message)

            myConn.Close()
            Exit Sub
        End Try

        myConn.Close()
        myConn.ConnectionString = "Server=(localdb)\MSSQLLocalDB;Database=visitas;Trusted_Connection=True;"
        myConn.Open()

        'Create a table in the new database
        myCommand.CommandText = "CREATE TABLE People (LastName VARCHAR(30), FirstName VARCHAR (30))"
        Try
            myCommand.ExecuteNonQuery()
            MessageBox.Show("Table is created successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        myConn.Close()
        myConn = Nothing
    End Sub
End Class

Thank you so much delano I will try it I really appreciate with all my hearth
 
Back
Top