This module create a database I need a statement to make it run from button click.
Module Module1
Sub Main()
' Define your connection string (update this based on your server and database)
Dim connectionString As String = ("Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;")
' Define the SQL command to create a new table
Dim createTableQuery As String = "
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
BirthDate DATE
)"
' Create and open a connection to SQL Express
Using connection As New SqlConnection(connectionString)
Try
' Open the connection
connection.Open()
' Create a command to execute the SQL query
Using command As New SqlCommand(createTableQuery, connection)
' Execute the command
command.ExecuteNonQuery()
Console.WriteLine("Table 'Employees' created successfully.")
End Using
Catch ex As Exception
' Handle any errors that may have occurred
Console.WriteLine("Error: " & ex.Message)
Finally
' Close the connection
connection.Close()
End Try
End Using
' Pause the console so you can see the result (only if you're using a Console App)
Console.ReadLine()
End Sub
End Module
This module create a database I need a statement to make it run from button click.
Module Module1
Sub Main()
' Define your connection string (update this based on your server and database)
Dim connectionString As String = ("Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;")
' Define the SQL command to create a new table
Dim createTableQuery As String = "
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
BirthDate DATE
)"
' Create and open a connection to SQL Express
Using connection As New SqlConnection(connectionString)
Try
' Open the connection
connection.Open()
' Create a command to execute the SQL query
Using command As New SqlCommand(createTableQuery, connection)
' Execute the command
command.ExecuteNonQuery()
Console.WriteLine("Table 'Employees' created successfully.")
End Using
Catch ex As Exception
' Handle any errors that may have occurred
Console.WriteLine("Error: " & ex.Message)
Finally
' Close the connection
connection.Close()
End Try
End Using
' Pause the console so you can see the result (only if you're using a Console App)
Console.ReadLine()
End Sub
End Module
Share
Improve this question
asked Dec 14, 2024 at 14:48
Manny SomarribaManny Somarriba
111 silver badge
2
|
1 Answer
Reset to default 0I would turn the module into a class, then you can create an object and execute the code that way.
Here's what it would look like:
Public Class MyObject
Public Sub Main()
' Define your connection string (update this based on your server and database)
Dim connectionString As String = ("Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;")
' Define the SQL command to create a new table
Dim createTableQuery As String = "
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
BirthDate DATE
)"
' Create and open a connection to SQL Express
Using connection As New SqlConnection(connectionString)
Try
' Open the connection
connection.Open()
' Create a command to execute the SQL query
Using command As New SqlCommand(createTableQuery, connection)
' Execute the command
command.ExecuteNonQuery()
Console.WriteLine("Table 'Employees' created successfully.")
End Using
Catch ex As Exception
' Handle any errors that may have occurred
Console.WriteLine("Error: " & ex.Message)
Finally
' Close the connection
connection.Close()
End Try
End Using
' Pause the console so you can see the result (only if you're using a Console App)
Console.ReadLine()
End Sub
End Class
And in Form1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Put any code here that when you start the program
End Sub
Private Sub CommandButton1_Click(sender As Object, e As EventArgs) Handles CommandButton1.Click
dim MyObject as new MyObject
MyObject.Main()
End Sub
End Class
Click
event handler. Of course, you should rename the method, becauseMain
is the name for the entry point for a program, which that is not in this case. You would also want to get rid of theConsole
interactions, as this will no longer be a Console app. Having said that, you could just copy the code from that method into yourClick
event handler. – jmcilhinney Commented Dec 15, 2024 at 5:38