一):数据库存储为int型,在textbox里显示“01”,之后根据数据库里ID的情况显示下一行ID,比如,数据库里的ID最大值为“06”,则新打开界面时要在界面上显示“07”:
--------------------
写在前面的代码:
Private ds As New DataSet
Dim comid1 As Int32
Dim comid2 As Int32
----------
Dim sql As String = "select max(表里的ID字段) from 表"
Dim adt As New OleDbDataAdapter(sql, DBcon)
ds.Clear()
adt.Fill(ds)
comid2 = ds.Tables(0).Rows(0)(0)
comid2 = comid2 + 1
Me.TextBox3.Text = comid2
此段代码要写在form_load 和 相应按钮里,一个也不能少!
------------------------------
二):数据库存储为char型,在textbox里显示“c01”,之后根据数据库里ID的情况显示下一行ID,比如,数据库里的ID最大值为“c06”,则新打开界面时要在界面上显示“c07”:
--------------------
写在前面的代码:
Private ds As New DataSet
Dim comid1 As Int32
Dim comid2 As Int32
Dim comchar As String
Dim comchar1 As String = "c"
Dim comchar2 As String = "0"
----------
Dim sql1 As String = "select count(表里的ID字段) as cTypeID from 表"
Dim adt As New OleDbDataAdapter(sql1, DBcon)
ds.Clear()
adt.Fill(ds)
comid1 = ds.Tables(0).Rows(0)(0)
comid1 = comid1 + 1
If comid1 <= 9 Then
comchar = comchar1 & comchar2 & comid1
Else
comchar = comchar1 & comid1
End If
Me.TextBox1.Text = comchar
此段代码要写在form_load 和 相应按钮里,一个也不能少!
------------------------------
三):数据库存储为char型,在textbox里显示“c01001”,之后根据数据库里ID的情况显示下一行ID,要根据下拉菜单选择表一里的name,再将其对应的ID(c01)显示在textbox里,后面的“001”为自增。(此代码的运行结果为后面三位一直在自增,即选择了c01001之后只能选择c02002,不能选择c02001)
--------------------
写在前面的代码:
Private ds As New DataSet
Dim comid As Int32
Dim comchar As String
Dim comchar1 As String = "0"
Dim kuid As String
---------
DBcon.Open()
Dim sqlStr As String = "select ID from TMMType where name = '" & Me.ComboBox.SelectedItem & "'"
Dim adt4 As New OleDbDataAdapter(sqlStr, DBcon)
ds.Clear()
adt.Fill(ds)
DBcon.Close()
kuid = Trim(CStr(ds.Tables(0).Rows(0).Item("ID")))
Dim sql1 As String = "select count(ID) as ID from TMMCom"
Dim adt5 As New OleDbDataAdapter(sql1, DBcon)
ds5.Clear()
adt5.Fill(ds5)
comid = ds5.Tables(0).Rows(0)(0)
comid = comid + 1
If comid <= 9 Then
comchar = kuid & comchar1 & comchar1 & comid
ElseIf 10 <= comid <= 99 Then
comchar = kuid & comchar1 & comid
ElseIf 100 <= comid <= 999 Then
comchar = kuid & comid
End If
Me.TextBox1.Text = comchar
以上代码写在相应按钮和 ComboBox的SelectedIndexChanged 事件里!(必须全写!)
----------------
三)做法二:(三层结构做的)
Public Function GetNewChangeStoreID(ByVal str As String) As String
Try
con = New OleDbConnection(constr)
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim sqlstr As String = "select Max(right(cID,3)) from TMM_ChangeStore"
cmd = New OleDbCommand(sqlstr, con)
Dim read As OleDbDataReader = cmd.ExecuteReader()
read.Read()
If Not read.Item(0) Is DBNull.Value Then
Dim num As Integer = read.Item(0)
Return "D" & str & (num + 1).ToString("D3")
Else
Return "D" & str & "001"
End If
Catch ex As Exception
MsgBox(ex.Message.ToString)
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Function