1,216
社区成员
发帖
与我相关
我的任务
分享
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cnStr As String = " Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xxxx;Data Source=xxxx"
cn = New SqlConnection(cnStr) '连接数据库
cn.Open()
Dim id, nowdata As String
nowdata = Format(DateTime.Now, "yyMMdd") '获取今天日期
'第一步,模糊查询现有最大编号
Dim str As String = "Select max(PatID) from current_Patient where PatID like'%" & nowdata & "%'"
da = New SqlDataAdapter(str, cn)
ds.Clear()
da.Fill(ds, "ss号")
DataGridView1.DataSource = ds.Tables("ss号") '测试查询编号
If ds.Tables("ss号").Rows.Count = 0 Then '判断编号是否为当天新号
id = Format(DateTime.Now, "yyMMdd").ToString * 1000 '第二步,没有返回值,建立新编号
TextBox2.Text = id + 1
Else
TextBox2.Text = ds.Tables("ss号").Rows(0).Item(0).ToString + 1 '有返回值,最大值编号+1
End If
cn.Close()
End Sub