我的计划有问题,我需要INCREMENT
某个Label
Dim p1num As Integer = 0
M.Text = Format(Now, "MM")
Y.Text = Format(Now, "yy")
txtPNumber.Text = Y.Text.ToString & M.Text.ToString & p1num.ToString("D4")
我用它来生成患者ID,其中M表示月份,Y表示年份+ p1num表示创建4位数字....所以输出将如下:
(让我们使用当前日期和年份)
13020001
13020002
13020003
13020004
依旧......
在此之后,我将使用这些代码将其插入SQL SERVER中的数据库:
Dim SQLcon As New SqlConnection
Dim SQLdr As SqlDataReader
Try
SQLcon.ConnectionString = "Data Source=####;" & _
"Initial Catalog=####;Persist Security Info=True;User ID=####;Password="
Dim SQLcmd As New SqlCommand("INSERT INTO dbo.Patients" & _
"(pIDNo)" & _
"VALUES(@pIDNo)", SQLcon)
SQLcmd.Parameters.AddWithValue("@pIDNo", txtPNumber.Text)
MsgBox("Patient Added!", MsgBoxStyle.Information)
SQLdr = SQLcmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show("Error Occured, Can't Add Patient!" & ex.Message)
Finally
SQLcon.Close()
End Try
Return ""
现在的问题是,当我关闭程序时,生成到TextBox
的值始终在13020001中开始。我想保留最近添加的值,然后将其增加到+1,所以我打算使用以下内容显示刚添加到我的数据库的当前ID:
Dim querystring As String = "SELECT MAX(pIDNo) FROM dbo.Patients"
Using connection As New SqlConnection("Data Source=####;" & _
"Initial Catalog=####;Persist Security Info=True;User ID=####;Password=")
Dim command As New SqlCommand(querystring, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader
While reader.Read
txtCurrentID.Text = reader.GetString(0)
End While
reader.Close()
我设法显示当前的ID,但现在的问题是我无法将其递增1。
其他问题:我尝试复制显示的值并将其添加一个,但另一个问题出现了,月份和年份没有变化。如果我到达'MARCH'月份,那么产生的价值仍然是
1302 + pnum1.text(“D4”),应该是1303 + pnum1.text(“D4”)
1302是FEBRUARY 1303适用于MARCH
任何人都可以解决我的问题吗?
答案 0 :(得分:1)
13020004这是最后插入的值。你能告诉我们你之后的所需价值吗?
答案 1 :(得分:1)
将MAX(pIDNo)
检索到String
值。
Dim value = string.Empty
While reader.Read
value = reader.GetString(0)
End While
之后,您可以根据需要解析value
:
dim year = value.Substring(0,2)
dim month = value.Substring(2,2)
dim patientId = value.Substring(4,4)
dim newPatientId as Integer = Integer.Parse(patientId) + 1
.......
修改强> 此代码可以为您提供您想要的内容:
txtCurrentID.Text = Today.Year.ToString().Substring(2,2) & Today.Month.ToString().PadLeft(2, "0") & (Integer.Parse(value) + 1).ToString("D4")
答案 2 :(得分:1)
While reader.Read //you don't need that for aggregate functions.
txtCurrentID.Text = command.ExecuteScaler() //something like that... is enough
int p1num=integer.parse(txt.CurrentID.text.substring(4,4)) +1
// End While
...
txtPNumber.Text = Y.Text.ToString & M.Text.ToString & p1num.ToString("D4")
如果您只想要日期:那么请使用Date.Today
pNumber.Text = Today.Date.Month.ToString + " " + Today.Date.Year.ToString
IntelliSense是您的第一本书,因此必须使用它。 ;)
答案 3 :(得分:1)
从数据库中检索患者ID并将其吐出,分配给另一个变量。 此方法有效
dim p1num = patientId.Substring(4,7)
Dim M = Format(Now, "MM")
Dim Y = Format(Now, "yy")
lblsample.Text = Y.ToString() & M.ToString() & p1num.ToString("D4")