Visual Basic 2010 数据库开发之超市管理系统05价格管理草稿

后台

Imports System.Data.SqlClient
Namespace tableclass
    '用户表类
    Public Class user
        Private mName As String
        Private mPassword As String
        Public Property Name As String
            Get
                Return mName
            End Get
            Set(ByVal value As String)
                mName = value
            End Set
        End Property
        Public Property Password As String
            Get
                Return mPassword
            End Get
            Set(ByVal value As String)
                mPassword = value
            End Set
        End Property

        Public Function GetAllUser() As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()
            Dim strSql As String = "Select * from usertable"
            Dim da As New SqlDataAdapter(strSql, conn)
            Dim dt As New DataTable
            da.Fill(dt)
            conn.Close()
            Return dt
        End Function

        Public Function GetByValue(ByVal strName As String, ByVal strpwd As String) As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()
            Dim strSql As String = "Select * from usertable where name='{0}' and password='{1}'"
            strSql = String.Format(strSql, strName, strpwd)
            Dim da As New SqlDataAdapter(strSql, conn)
            Dim dt As New DataTable
            da.Fill(dt)
            conn.Close()
            Return dt
        End Function

        '添加
        Public Sub Add()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = "INSERT INTO usertable (name,password) VALUES (@name,@password)"
                Dim comm As SqlCommand = New SqlCommand(sql, conn)
                Dim p1 As SqlParameter = New SqlParameter("@name", Name)
                Dim p2 As SqlParameter = New SqlParameter("@phone", Password)

                comm.Parameters.Add(p1)
                comm.Parameters.Add(p2)

                comm.ExecuteNonQuery()

            Finally
                conn.Close()
            End Try
        End Sub

        '更新
        Public Sub Update()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = "UPDATE usertable SET password=@password WHERE username=" + Name
                Dim comm As SqlCommand = New SqlCommand(sql, conn)
                Dim p1 As SqlParameter = New SqlParameter("@name", Name)
                Dim p2 As SqlParameter = New SqlParameter("@phone", Password)
                comm.Parameters.Add(p1)
                comm.Parameters.Add(p2)
                comm.ExecuteNonQuery()
            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub

        '删除
        Public Sub Delete(ByVal nm As String)
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = String.Format("DELETE FROM usertable WHERE name={0}", Name)
                Dim comm As SqlCommand = New SqlCommand(sql, conn)
                comm.ExecuteNonQuery()

            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub
    End Class
    '进货表类
    Public Class JinHuo
        Private mjhID As String
        Private mjhDate As Date
        Private mjhZhongLei As String
        Private mjhHPID As String
        Private mjhMingCheng As String
        Private mjhDanWei As String
        Private mjhDanJia As Decimal
        Private mjhCount As Integer
        Private mjhJinE As Decimal
        Private mjhJinShouRen As String
        Private mjhGongYingShang As String

        Public Property jhID As String
            Get
                Return mjhID
            End Get
            Set(ByVal value As String)
                mjhID = value
            End Set
        End Property
        Public Property jhDate As Date
            Get
                Return mjhDate
            End Get
            Set(ByVal value As Date)
                mjhDate = value
            End Set
        End Property
        Public Property jhZhongLei As String
            Get
                Return mjhZhongLei
            End Get
            Set(ByVal value As String)
                mjhZhongLei = value
            End Set
        End Property
        Public Property jhHPID As String
            Get
                Return mjhHPID
            End Get
            Set(ByVal value As String)
                mjhHPID = value
            End Set
        End Property
        Public Property jhMingCheng As String
            Get
                Return mjhMingCheng
            End Get
            Set(ByVal value As String)
                mjhMingCheng = value
            End Set
        End Property
        Public Property jhDanWei As String
            Get
                Return mjhDanWei
            End Get
            Set(ByVal value As String)
                mjhDanWei = value
            End Set
        End Property
        Public Property jhDanJia As Decimal
            Get
                Return mjhDanJia
            End Get
            Set(ByVal value As Decimal)
                mjhDanJia = value
            End Set
        End Property
        Public Property jhCount As Integer
            Get
                Return mjhCount
            End Get
            Set(ByVal value As Integer)
                mjhCount = value
            End Set
        End Property
        Public Property jhJinE As Decimal
            Get
                Return mjhJinE
            End Get
            Set(ByVal value As Decimal)
                mjhJinE = value
            End Set
        End Property
        Public Property jhJingShouRen As String
            Get
                Return mjhJinShouRen
            End Get
            Set(ByVal value As String)
                mjhJinShouRen = value
            End Set
        End Property
        Public Property jhGongYingShang As String
            Get
                Return mjhGongYingShang
            End Get
            Set(ByVal value As String)
                mjhGongYingShang = value
            End Set
        End Property

        Public Function GetAllJinHouTable() As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()

            Dim strSql As String = "Select * from jhTable"
            Dim jhda As New SqlDataAdapter(strSql, conn)
            Dim jhdt As New DataTable
            jhda.Fill(jhdt)

            conn.Close()
            Return jhdt
        End Function

        Public Sub Add()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = _
                    "INSERT INTO jhTable" _
                  & "(票据号,进货日期,货品种类,货品编号,货品名称,单位,进货单价,进货数量,金额,经手人,供应商) " _
                  & "VALUES " _
                  & "(@票据号,@进货日期,@货品种类,@货品编号,@货品名称,@单位,@进货单价,@进货数量,@金额,@经手人,@供应商)"

                'MsgBox(sql)'检查语法用的

                Dim cmd As SqlCommand = New SqlCommand(sql, conn)

                Dim p1 As SqlParameter = New SqlParameter("@票据号", jhID)
                Dim p2 As SqlParameter = New SqlParameter("@进货日期", jhDate)
                Dim p3 As SqlParameter = New SqlParameter("@货品种类", jhZhongLei)
                Dim p4 As SqlParameter = New SqlParameter("@货品编号", jhHPID)
                Dim p5 As SqlParameter = New SqlParameter("@货品名称", jhMingCheng)
                Dim p6 As SqlParameter = New SqlParameter("@单位", jhDanWei)
                Dim p7 As SqlParameter = New SqlParameter("@进货单价", jhDanJia)
                Dim p8 As SqlParameter = New SqlParameter("@进货数量", jhCount)
                Dim p9 As SqlParameter = New SqlParameter("@金额", jhJinE)
                Dim p10 As SqlParameter = New SqlParameter("@经手人", jhJingShouRen)
                Dim p11 As SqlParameter = New SqlParameter("@供应商", jhGongYingShang)

                cmd.Parameters.Add(p1)
                cmd.Parameters.Add(p2)
                cmd.Parameters.Add(p3)
                cmd.Parameters.Add(p4)
                cmd.Parameters.Add(p5)
                cmd.Parameters.Add(p6)
                cmd.Parameters.Add(p7)
                cmd.Parameters.Add(p8)
                cmd.Parameters.Add(p9)
                cmd.Parameters.Add(p10)
                cmd.Parameters.Add(p11)

                cmd.ExecuteNonQuery()

            Finally
                conn.Close()
            End Try
        End Sub

        '更新
        Public Sub Update()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim strsql As String = _
                    "UPDATE jhTable " _
                  & "SET 票据号=@票据号,进货日期=@进货日期,货品种类=@货品种类," _
                  & "货品编号=@货品编号,货品名称=@货品名称,单位=@单位,进货单价=@进货单价," _
                  & "进货数量=@进货数量,金额=@金额,经手人=@经手人,供应商=@供应商 " _
                  & "WHERE 票据号='" + jhID + "'"

                MsgBox(strsql)

                Dim cmd As SqlCommand = New SqlCommand(strsql, conn)

                Dim p1 As SqlParameter = New SqlParameter("@票据号", jhID)
                Dim p2 As SqlParameter = New SqlParameter("@进货日期", jhDate)
                Dim p3 As SqlParameter = New SqlParameter("@货品种类", jhZhongLei)
                Dim p4 As SqlParameter = New SqlParameter("@货品编号", jhHPID)
                Dim p5 As SqlParameter = New SqlParameter("@货品名称", jhMingCheng)
                Dim p6 As SqlParameter = New SqlParameter("@单位", jhDanWei)
                Dim p7 As SqlParameter = New SqlParameter("@进货单价", jhDanJia)
                Dim p8 As SqlParameter = New SqlParameter("@进货数量", jhCount)
                Dim p9 As SqlParameter = New SqlParameter("@金额", jhJinE)
                Dim p10 As SqlParameter = New SqlParameter("@经手人", jhJingShouRen)
                Dim p11 As SqlParameter = New SqlParameter("@供应商", jhGongYingShang)

                cmd.Parameters.Add(p1)
                cmd.Parameters.Add(p2)
                cmd.Parameters.Add(p3)
                cmd.Parameters.Add(p4)
                cmd.Parameters.Add(p5)
                cmd.Parameters.Add(p6)
                cmd.Parameters.Add(p7)
                cmd.Parameters.Add(p8)
                cmd.Parameters.Add(p9)
                cmd.Parameters.Add(p10)
                cmd.Parameters.Add(p11)

                cmd.ExecuteNonQuery()
            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub

        '删除
        Public Sub Delete(ByVal jhid As String)
            MsgBox(jhid)
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()

                Dim sql As String = String.Format("DELETE FROM jhTable WHERE 票据号='{0}'", jhid)
                Dim cmd As SqlCommand = New SqlCommand(sql, conn)
                cmd.ExecuteNonQuery()

            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub

    End Class
    '价格类
    Public Class jiage
        Private mjgZhongLei As String
        Private mjgMingCheng As String
        Private mjgBianHao As String
        Private mjgJinJia As Decimal
        Private mjgShoujia As Decimal
        Public Property jgZhongLei As String
            Get
                Return mjgZhongLei
            End Get
            Set(ByVal value As String)
                mjgZhongLei = value
            End Set
        End Property
        Public Property jgMingCheng As String
            Get
                Return mjgMingCheng
            End Get
            Set(ByVal value As String)
                mjgMingCheng = value
            End Set
        End Property
        Public Property jgBianHao As String
            Get
                Return mjgBianHao
            End Get
            Set(ByVal value As String)
                mjgBianHao = value
            End Set
        End Property
        Public Property jgJinJia As Decimal
            Get
                Return mjgJinJia
            End Get
            Set(ByVal value As Decimal)
                mjgJinJia = value
            End Set
        End Property
        Public Property jgShoujia As Decimal
            Get
                Return mjgShoujia
            End Get
            Set(ByVal value As Decimal)
                mjgShoujia = value
            End Set
        End Property

        Public Function GetAllJiaGeTable() As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()
            Dim strSql As String = "Select * from jgTable"
            Dim da As New SqlDataAdapter(strSql, conn)
            Dim dt As New DataTable
            da.Fill(dt)
            conn.Close()
            Return dt
        End Function
        Public Function GetZhongLei() As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()

            Dim strSql As String = "Select 货品种类  from jhTable"
            Dim da As New SqlDataAdapter(strSql, conn)
            Dim dt As New DataTable
            da.Fill(dt)

            conn.Close()
            Return dt
        End Function
        Public Function GetByZhongLei(ByVal zhl As String) As DataTable
            Dim conn As SqlConnection = GetConnection()
            conn.Open()

            Dim strSql As String = "Select 货品名称,货品编号,进货单价 " _
                                 & "From jhTable " _
                                 & "Where 货品种类 like '%{0}%'"
            strSql = String.Format(strSql, zhl)
            Dim da As New SqlDataAdapter(strSql, conn)
            Dim dt As New DataTable
            da.Fill(dt)
            Return dt

            conn.Close()
        End Function

        Public Sub Add()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = _
                    "INSERT INTO jgTable" _
                  & "(货品种类,货品名称,货品编号,进价,售价) " _
                  & "VALUES " _
                  & "(@货品种类,@货品名称,@货品编号,@进价,@售价)"

                'MsgBox(sql)'检查语法用的

                Dim cmd As SqlCommand = New SqlCommand(sql, conn)

                Dim p1 As SqlParameter = New SqlParameter("@货品种类", jgZhongLei)
                Dim p2 As SqlParameter = New SqlParameter("@货品名称", jgMingCheng)
                Dim p3 As SqlParameter = New SqlParameter("@货品编号", jgBianHao)
                Dim p4 As SqlParameter = New SqlParameter("@进价", jgJinJia)
                Dim p5 As SqlParameter = New SqlParameter("@售价", jgShoujia)

                cmd.Parameters.Add(p1)
                cmd.Parameters.Add(p2)
                cmd.Parameters.Add(p3)
                cmd.Parameters.Add(p4)
                cmd.Parameters.Add(p5)

                cmd.ExecuteNonQuery()

            Finally
                conn.Close()
            End Try
        End Sub

        '更新
        Public Sub Update()
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim strsql As String = _
                    "UPDATE jgTable " _
                  & "SET 货品种类=@货品种类,货品编号=@货品编号,货品名称=@货品名称,进价=@进价,售价=@售价 " _
                  & "WHERE 货品编号='" + jgBianHao + "'"

                'MsgBox(strsql)

                Dim cmd As SqlCommand = New SqlCommand(strsql, conn)

                Dim p1 As SqlParameter = New SqlParameter("@货品种类", jgZhongLei)
                Dim p2 As SqlParameter = New SqlParameter("@货品编号", jgBianHao)
                Dim p3 As SqlParameter = New SqlParameter("@货品名称", jgMingCheng)
                Dim p4 As SqlParameter = New SqlParameter("@进价", jgJinJia)
                Dim p5 As SqlParameter = New SqlParameter("@售价", jgShoujia)

                cmd.Parameters.Add(p1)
                cmd.Parameters.Add(p2)
                cmd.Parameters.Add(p3)
                cmd.Parameters.Add(p4)
                cmd.Parameters.Add(p5)

                cmd.ExecuteNonQuery()
            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub

        '删除
        Public Sub Delete(ByVal bianhao As String)
            Dim conn As SqlConnection = GetConnection()
            Try
                conn.Open()
                Dim sql As String = String.Format("DELETE FROM jgTable WHERE 货品编号='{0}'", bianhao)
                Dim cmd As SqlCommand = New SqlCommand(sql, conn)
                cmd.ExecuteNonQuery()

            Catch ex As Exception
            Finally
                conn.Close()
            End Try
        End Sub
    End Class
End Namespace
 

客户端

Imports System.Data.SqlClient
Imports 超市管理系统.tableclass
Public Class FrmPrice
    Dim flag As String = ""
    Dim newjiage As New jiage
    Dim jgdt As DataTable
    '使用dataview定制从数据库返回的、存储在DataSet中的记录的视图
    Dim jgDataView As DataView
    'CurrencyManager用于控制绑定数据
    Dim bmdata As CurrencyManager

    '数据填充并更新显示过程
    Private Sub FillDataSetAndView()
        jgdt = New DataTable
        jgdt = newjiage.GetAllJiaGeTable
        '将表填入dataview
        jgDataView = New DataView(jgdt)
        '将DataView变量绑定控件,能记录位置
        bmdata = Me.BindingContext(jgDataView)
    End Sub

    '获得数据库最新数据,并填充DataView对象
    Private Sub BindFields()
        cmbZhongLei.DataBindings.Clear()
        cmbMingCheng.DataBindings.Clear()
        cmbBianHao.DataBindings.Clear()
        txtJinJia.DataBindings.Clear()
        txtShouJia.DataBindings.Clear()
        

        'Add三个参数:控件属性,(DataSet,Datatable,DataView),数据字段
        cmbZhongLei.DataBindings.Add("text", jgDataView, "货品种类")
        cmbMingCheng.DataBindings.Add("text", jgDataView, "货品名称")
        cmbBianHao.DataBindings.Add("text", jgDataView, "货品编号")
        txtJinJia.DataBindings.Add("text", jgDataView, "进价")
        txtShouJia.DataBindings.Add("text", jgDataView, "售价")
        
    End Sub

    '设置控件可用
    Private Sub SetControlEnable()
        cmbZhongLei.Enabled = True
        cmbMingCheng.Enabled = True
        cmbBianHao.Enabled = True
        txtJinJia.Enabled = True
        txtShouJia.Enabled = True
        
    End Sub
    '设置控件不可用
    Private Sub SetControlDisable()
        cmbZhongLei.Enabled = False
        cmbMingCheng.Enabled = False
        cmbBianHao.Enabled = False
        txtJinJia.Enabled = False
        txtShouJia.Enabled = False
    End Sub

    Private Sub FrmPrice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetControlDisable()

        newjiage = New jiage
        Dim zhldt As New DataTable
        zhldt = newjiage.GetZhongLei
        For Each row As DataRow In zhldt.Rows
            cmbZhongLei.Items.Add(row.Item(0))
        Next

        FillDataSetAndView()

        BindFields()

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        DataGridView1.DataSource = jgdt
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        SetControlEnable()
        flag = "add"
        'bmdata.EndCurrentEdit()
        'bmdata.AddNew()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        newjiage = New jiage
        newjiage.jgZhongLei = cmbZhongLei.Text
        newjiage.jgMingCheng = cmbMingCheng.Text
        newjiage.jgBianHao = cmbBianHao.Text
        newjiage.jgJinJia = txtJinJia.Text
        newjiage.jgShoujia = txtShouJia.Text
        If flag = "add" Then
            newjiage.Add()
        ElseIf flag = "modify" Then
            newjiage.Update()
        Else
            SetControlDisable()
            Exit Sub
        End If
        flag = ""

        FillDataSetAndView()
        BindFields()

        DataGridView1.DataSource = jgdt

        SetControlDisable()

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        flag = "modify"
        SetControlEnable()
        cmbBianHao.Focus()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If MessageBox.Show("确定删除吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = DialogResult.Yes Then
            newjiage = New jiage
            If bmdata.Count > 0 Then
                newjiage.Delete(cmbBianHao.Text)
                jgdt.Rows(bmdata.Position).Delete()
            End If
        End If

        FillDataSetAndView()
        BindFields()

        DataGridView1.DataSource = jgdt
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim index As Integer = e.RowIndex
        bmdata.Position = index
        BindFields()

        cmbZhongLei.Text = DataGridView1.Rows(index).Cells(0).Value
        cmbMingCheng.Text = DataGridView1.Rows(index).Cells(1).Value
        cmbBianHao.Text = DataGridView1.Rows(index).Cells(2).Value
        txtJinJia.Text = DataGridView1.Rows(index).Cells(3).Value
        txtShouJia.Text = DataGridView1.Rows(index).Cells(4).Value
    End Sub

    Private Sub cmbZhongLei_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbZhongLei.TextChanged
        newjiage = New jiage
        Dim dt As New DataTable
        dt = newjiage.GetByZhongLei(cmbZhongLei.Text)
        'MsgBox(dt.Rows.Count)
        cmbMingCheng.Text = dt.Rows(0).Item(0)
        cmbBianHao.Text = dt.Rows(0).Item(1)
        txtJinJia.Text = dt.Rows(0).Item(2)
    End Sub
End Class

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值