目录
遇到难题了,整不明白表关系和联合查询,currentcymanage又该怎么用呢?各种没见过的调试错误。。。SSMS说没有所有者,不能建关系,把数据库加入项目,数据库一会儿更新,一会儿不更新,啥啥啥的
界面
后台
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 GetItemById(ByVal id As String) As DataTable
Dim conn As SqlConnection = GetConnection()
conn.Open()
Dim strSql As String = "Select * from jgTable Where jhid={0}"
strSql = String.Format(strSql, id)
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 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 FrmJinHuo
Dim flag As String = ""
Dim newjinhuo As New JinHuo
Dim jhdt As DataTable
'使用dataview定制从数据库返回的、存储在DataSet中的记录的视图
Dim jhDataView As DataView
'CurrencyManager用于控制绑定数据
Dim bmdata As CurrencyManager
'数据填充并更新显示过程
Private Sub FillDataSetAndView()
jhdt = New DataTable
jhdt = newjinhuo.GetAllJinHouTable
'将表填入dataview
jhDataView = New DataView(jhdt)
'将DataView变量绑定控件,能记录位置
bmdata = Me.BindingContext(jhDataView)
End Sub
'获得数据库最新数据,并填充DataView对象
Private Sub BindFields()
txtPiaoJu.DataBindings.Clear()
DateTimePicker1.DataBindings.Clear()
cmbzhonglei.DataBindings.Clear()
txtBianHao.DataBindings.Clear()
txtMingCheng.DataBindings.Clear()
txtDanWei.DataBindings.Clear()
txtDanJia.DataBindings.Clear()
txtCount.DataBindings.Clear()
txtJinE.DataBindings.Clear()
txtJingShouRen.DataBindings.Clear()
txtGongYing.DataBindings.Clear()
'Add三个参数:控件属性,(DataSet,Datatable,DataView),数据字段
txtPiaoJu.DataBindings.Add("text", jhDataView, "票据号")
DateTimePicker1.DataBindings.Add("value", jhDataView, "进货日期")
cmbzhonglei.DataBindings.Add("text", jhDataView, "货品种类")
txtBianHao.DataBindings.Add("text", jhDataView, "货品编号")
txtMingCheng.DataBindings.Add("text", jhDataView, "货品名称")
txtDanWei.DataBindings.Add("text", jhDataView, "单位")
txtDanJia.DataBindings.Add("text", jhDataView, "进货单价")
txtCount.DataBindings.Add("text", jhDataView, "进货数量")
txtJinE.DataBindings.Add("text", jhDataView, "金额")
txtJingShouRen.DataBindings.Add("text", jhDataView, "经手人")
txtGongYing.DataBindings.Add("text", jhDataView, "供应商")
End Sub
'设置控件可用
Private Sub SetControlEnable()
txtPiaoJu.Enabled = True
DateTimePicker1.Enabled = True
cmbzhonglei.Enabled = True
txtBianHao.Enabled = True
txtMingCheng.Enabled = True
txtDanWei.Enabled = True
txtDanJia.Enabled = True
txtCount.Enabled = True
txtJinE.Enabled = True
txtJingShouRen.Enabled = True
txtGongYing.Enabled = True
End Sub
'设置控件不可用
Private Sub SetControlDisable()
txtPiaoJu.Enabled = False
DateTimePicker1.Enabled = False
cmbzhonglei.Enabled = False
txtBianHao.Enabled = False
txtMingCheng.Enabled = False
txtDanWei.Enabled = False
txtDanJia.Enabled = False
txtCount.Enabled = False
txtJinE.Enabled = False
txtJingShouRen.Enabled = False
txtGongYing.Enabled = False
End Sub
Private Sub testdebug()
'调试用
txtPiaoJu.Text = "234"
DateTimePicker1.Value = Today
cmbzhonglei.Text = "dsf"
txtBianHao.Text = "dsf"
txtMingCheng.Text = "dsf"
txtDanWei.Text = "dsf"
txtDanJia.Text = "2"
txtCount.Text = "3"
txtJinE.Text = "6"
txtJingShouRen.Text = "dsf"
txtGongYing.Text = "dsf"
End Sub
Private Sub FrmJinHuo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
testdebug()
SetControlDisable()
FillDataSetAndView()
BindFields()
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
DataGridView1.DataSource = jhdt
End Sub
'登记
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
flag = "add"
SetControlEnable()
Try
bmdata.EndCurrentEdit()
bmdata.AddNew()
txtJingShouRen.Text = username
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'保存
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim newjinhuo As New JinHuo
newjinhuo.jhID = BindingContext(jhDataView).Current("票据号")
newjinhuo.jhDate = DateTimePicker1.Value
newjinhuo.jhZhongLei = cmbzhonglei.Text
newjinhuo.jhHPID = txtBianHao.Text
newjinhuo.jhMingCheng = txtMingCheng.Text
newjinhuo.jhDanWei = txtDanWei.Text
newjinhuo.jhDanJia = txtDanJia.Text
newjinhuo.jhCount = txtCount.Text
newjinhuo.jhJinE = txtJinE.Text
newjinhuo.jhJingShouRen = txtJingShouRen.Text
newjinhuo.jhGongYingShang = txtGongYing.Text
If flag = "add" Then
newjinhuo.Add()
ElseIf flag = "modify" Then
newjinhuo.Update()
Else
SetControlDisable()
Exit Sub
End If
flag = ""
FillDataSetAndView()
BindFields()
DataGridView1.DataSource = jhdt
SetControlDisable()
End Sub
'修改
Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
flag = "modify"
SetControlEnable()
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
If bmdata.Count > 0 Then
newjinhuo.Delete(txtPiaoJu.Text)
jhdt.Rows(bmdata.Position).Delete()
End If
End If
FillDataSetAndView()
BindFields()
DataGridView1.DataSource = jhdt
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 txtBianHao_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBianHao.Validated
Dim conn As SqlConnection = GetConnection()
conn.Open()
Dim strSql As String = "Select 货品名称,单位,进货单价,供应商 from jhTable " _
& "Where 货品编号='" & txtBianHao.Text.Trim & "'"
Dim comm As New SqlCommand(strSql, conn)
Dim dr As SqlDataReader
dr = comm.ExecuteReader
While dr.Read
Try
txtMingCheng.Text = dr.GetString(0)
txtDanWei.Text = dr.GetString(1)
txtDanJia.Text = dr.GetDecimal(2)
txtGongYing.Text = dr.GetString(3)
Catch ex As Exception
MsgBox(Err.Description)
End Try
End While
conn.Close()
End Sub
'智能计算金额
Private Sub txtDanJia_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDanJia.TextChanged
txtJinE.Text = Val(txtDanJia.Text) * Val(txtCount.Text)
End Sub
Private Sub txtCount_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCount.TextChanged
txtJinE.Text = Val(txtDanJia.Text) * Val(txtCount.Text)
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
SetControlDisable()
Dim index As Integer = e.RowIndex
bmdata.Position = index
txtPiaoJu.Text = DataGridView1.Rows(index).Cells(0).Value
DateTimePicker1.Text = DataGridView1.Rows(index).Cells(1).Value
cmbzhonglei.Text = DataGridView1.Rows(index).Cells(2).Value
txtBianHao.Text = DataGridView1.Rows(index).Cells(3).Value
txtMingCheng.Text = DataGridView1.Rows(index).Cells(4).Value
txtDanWei.Text = DataGridView1.Rows(index).Cells(5).Value
txtDanJia.Text = DataGridView1.Rows(index).Cells(6).Value
txtCount.Text = DataGridView1.Rows(index).Cells(7).Value
txtJingShouRen.Text = DataGridView1.Rows(index).Cells(9).Value
txtGongYing.Text = DataGridView1.Rows(index).Cells(10).Value
End Sub
End Class