如何生成自动ID [英] How to generate auto ID
本文介绍了如何生成自动ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助解决一个问题
我想生成一个这样的自动ID,其中ARA = ARA然后显示结果如ARA004 + 1 = ARA005但显示从字符串ARA001的错误转换类型'整数'无效。详情请见下面
Please Help to solve one kind problem
I want generate an auto id like this where ARA = ARA then display result like ARA004+1= ARA005 but displayed an error conversion from string "ARA001" to type 'Integer' is not valid. See details below
Shop_No Block
ARA001 ARA
ARA002 ARA
ARA003 ARA
ARA004 ARA
BRA001 BRA
BRA002 BRA
BRA003 BRA
BRA004 BRA
CRA001 CRA
CRA002 CRA
CRA003 CRA
CRA004 CRA
CRA005 CRA
Shop_No为访问数据库中的文本设置字段并将我的dim更改为字符串仍显示错误显示从字符串ARA001转换为类型'String '无效
我尝试过:
以下是我试过的代码
Shop_No Field set for text in access database and I change my dim to string still error displayed conversion from string "ARA001" to type 'String' is not valid
What I have tried:
below is my tried codes
Dim IncrID As Integer
CustAutoSqlstr = "Select *From tblRevenueRegister WHERE Block='" & cbBlock.Text & "'"
CustAutodr = CustAutocmd.ExecuteReader()
If IncrID < CustAutodr.Item(0) Then
IncrID = CustAutodr("Shop_No").ToString
End If
txtShopNo.Text = IncrID + 1
推荐答案
Dim maxValue As Object
Dim block As String = cbBlock.Text
Using con As New SqlConnection("...")
Using cmd As New SqlCommand("SELECT Max(Shop_No) FROM tblRevenueRegister WHERE Block = @Block")
cmd.Parameters.AddWithValue("@Block", block)
con.Open()
maxValue = cmd.ExecuteScalar()
End Using
End Using
If maxValue Is Nothing OrElse Convert.IsDBNull(maxValue) Then
txtShopNo.Text = "1"
Else
Dim numberPart As String = Convert.ToString(maxValue).Substring(block.Length)
Dim shopNumber As Integer = Convert.ToInt32(numberPart)
txtShopNo.Text = (shopNumber + 1).ToString()
End If
关于SQL注入你想知道的一切(但不敢问)特洛伊亨特 [ ^ ]
如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]
查询参数化备忘单| OWASP [ ^ ]
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
在多用户环境中,您的代码将生成重复的Shop_no
。别这样做!您必须在服务器端创建自定义自动生成的序列!
请参阅:使用SQL Server自定义自动生成的序列 - SQLTeam.com [ ^ ]
Google的更多内容: sql autonumber基于另一个字段 - Google搜索 [ ^ ]
In multiuser environment your code will generate duplicatedShop_no
's. Stop doing this that way! You have to create custom-auto-generated sequence on server side!
See: Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[^]
More at Google: sql autonumber based on another field - Google Search[^]
这篇关于如何生成自动ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文