最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - I need to make run a module from button click, and I do not the code to put in button click in VB.Net? - Stack Overfl

programmeradmin16浏览0评论

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 You simply call that method from the Click event handler. Of course, you should rename the method, because Main 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 the Console interactions, as this will no longer be a Console app. Having said that, you could just copy the code from that method into your Click event handler. – jmcilhinney Commented Dec 15, 2024 at 5:38
  • I would never run that code to begin with. It doesn't create a database, it creates a table in database master, which is something you do not want to do. Part of programming is understanding code you copy from somewhere else, lest you have side affects you didn't count on. – HardCode Commented Dec 17, 2024 at 15:54
Add a comment  | 

1 Answer 1

Reset to default 0

I 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


与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论