如何利用VBA编写进销存管理系统的源码?

进销存VB源码是一种用于管理企业库存、销售和采购的计算机程序源代码。
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: 这行代码将数据加载到表“Database1DataSet.Products”中,您可以根据需要移动或删除它。
        Me.ProductsTableAdapter.Fill(Me.Database1DataSet.Products)
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim productName As String = txtProductName.Text
        Dim quantity As Integer = CInt(txtQuantity.Text)
        Dim price As Decimal = CDec(txtPrice.Text)
        Dim connectionString As String = "Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"
        Dim query As String = "INSERT INTO Products (ProductName, Quantity, Price) VALUES (@ProductName, @Quantity, @Price)"
        Using connection As New SqlConnection(connectionString)
            Using command As New SqlCommand(query, connection)
                command.Parameters.AddWithValue("@ProductName", productName)
                command.Parameters.AddWithValue("@Quantity", quantity)
                command.Parameters.AddWithValue("@Price", price)
                Try
                    connection.Open()
                    command.ExecuteNonQuery()
                    MessageBox.Show("产品添加成功!")
                Catch ex As Exception
                    MessageBox.Show("添加产品时出错:" & ex.Message)
                End Try
            End Using
        End Using
        Me.ProductsTableAdapter.Fill(Me.Database1DataSet.Products)
    End Sub
    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim selectedProduct As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
        If selectedProduct.Count > 0 Then
            Dim productId As Integer = CInt(selectedProduct(0).Cells("ProductId").Value)
            Dim newQuantity As Integer = CInt(txtNewQuantity.Text)
            Dim newPrice As Decimal = CDec(txtNewPrice.Text)
            Dim connectionString As String = "Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"
            Dim query As String = "UPDATE Products SET Quantity = @NewQuantity, Price = @NewPrice WHERE ProductId = @ProductId"
            Using connection As New SqlConnection(connectionString)
                Using command As New SqlCommand(query, connection)
                    command.Parameters.AddWithValue("@ProductId", productId)
                    command.Parameters.AddWithValue("@NewQuantity", newQuantity)
                    command.Parameters.AddWithValue("@NewPrice", newPrice)
                    Try
                        connection.Open()
                        command.ExecuteNonQuery()
                        MessageBox.Show("产品更新成功!")
                    Catch ex As Exception
                        MessageBox.Show("更新产品时出错:" & ex.Message)
                    End Try
                End Using
            End Using
            Me.ProductsTableAdapter.Fill(Me.Database1DataSet.Products)
        Else
            MessageBox.Show("请选择一个产品进行更新!")
        End If
    End Sub
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim selectedProduct As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
        If selectedProduct.Count > 0 Then
            Dim productId As Integer = CInt(selectedProduct(0).Cells("ProductId").Value)
            Dim connectionString As String = "Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"
            Dim query As String = "DELETE FROM Products WHERE ProductId = @ProductId"
            Using connection As New SqlConnection(connectionString)
                Using command As New SqlCommand(query, connection)
                    command.Parameters.AddWithValue("@ProductId", productId)
                    Try
                        connection.Open()
                        command.ExecuteNonQuery()
                        MessageBox.Show("产品删除成功!")
                    Catch ex As Exception
                        MessageBox.Show("删除产品时出错:" & ex.Message)
                    End Try
                End Using
            End Using
            Me.ProductsTableAdapter.Fill(Me.Database1DataSet.Products)
        Else
            MessageBox.Show("请选择一个产品进行删除!")
        End If
    End Sub
End Class

这个源码使用了VB.NET和SQL Server数据库,实现了简单的进销存管理系统,主要功能包括添加、更新和删除产品信息,在窗体加载时,会从数据库中读取产品信息并显示在DataGridView中,用户可以通过点击按钮来执行相应的操作。

如何利用VBA编写进销存管理系统的源码?

到此,以上就是小编对于“进销存vb源码”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。

如何利用VBA编写进销存管理系统的源码?

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1127748.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
未希新媒体运营
上一篇 2024-10-02 13:30
下一篇 2024-10-02 13:30

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入