[英]SQL Union query error with vb.net
The code was working great for a long time until somebody added new name(SE01) in SQL database that cause web program to crash. 该代码在很长一段时间内一直有效,直到有人在SQL数据库中添加了导致Web程序崩溃的新名称(SE01)。 The web error shown that
网络错误表明
System.InvalidCastException: Conversion from string "SE01" to type 'Integer'
is not valid.
along with other SQL Union query error, 以及其他SQL Union查询错误,
SELECT Substring([Name],@lenSrv, 8) as Name
FROM [dbo].[ServerOwners]
where [Name] like @Srv
and Name not like '%j%'
union
SELECT Substring([Server],@lenSrv, 8) as Name
FROM [dbo].[AuditLog]
where log='delete'
and [DATE] > (GETDATE() - 60)
and [SERVER] like @Srv
and Server not like '%j%'
order by [name]
The codes job is to find next available name through database using query for example, The exist servername in database is UXVP001, and the code will find the free one which it will be UXVP002. 代码工作是使用查询来通过数据库查找下一个可用名称,例如,数据库中的现有服务器名称为UXVP001,代码将找到可用的免费名称为UXVP002。 Now someone added UXVPSE01 in SQL database that seems to cause reading query to crash.
现在有人在SQL数据库中添加了UXVPSE01,这似乎导致读取查询崩溃。 I want it to be accepted/ignored new name without error.
我希望它被接受/忽略新名称而不会出现错误。
Here is snipped vb codes that search through database, 这是在数据库中搜索的简短vb代码,
srv = "UXPV"
sqlAddOn = "and Name not like '%j%'"
sqlAddOnAudit = "and Server not like '%j%'"
"Logic to find next available name"
"1. select the numbers to the right of the characters"
"2. loop all values and find first missing number"
Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
Dim strSql As String = "SELECT Substring([Name],@lenSrv, 8) as Name FROM [dbo].[ServerOwners] where [Name] like @Srv " & sqlAddOn
strSql &= " union "
strSql &= "SELECT Substring([Server],@lenSrv, 8) as Name FROM [dbo].[AuditLog] where log='delete' and [DATE] > (GETDATE() - 60) and [SERVER] like @Srv " & sqlAddOnAudit
strSql &= " order by [name]"
Dim cmd As New System.Data.SqlClient.SqlCommand(strSql, sqlConn)
Dim dr As System.Data.SqlClient.SqlDataReader
LabelError.Text = ""
Dim x As Integer = 1
Dim y As Integer = 1
Dim foundYet As Boolean = False
Try
sqlConn.Open()
cmd.Parameters.AddWithValue("@lenSrv", srv.Length + 1)
cmd.Parameters.AddWithValue("@Srv", srv & "%")
dr = cmd.ExecuteReader()
While dr.Read() And foundYet = False
LabelError.Text = LabelError.Text & dr("Name") & " | "
y = CType(dr("Name"), Integer)
If x = y Then
"keep going"
x = x + 1
Else
"you found first available number"
foundYet = True
End If
End While
dr.Close()
cmd.Dispose()
Catch ex As Exception
hide()
PanelError.Visible = True
LabelError.Text = ex.ToString() & "<hr/>" & strSql
Finally
sqlConn.Dispose()
End Try
"make sure leading zeros are present"
" 000"
Dim fmt As String = "00#"
tbAdd_ServerName.Text = srv & x.ToString(fmt)
tbAdd_ServerName.Enabled = False
tbAdd_TM.SelectedValue = "*"
Of course this doesn't work. 当然这是行不通的。 You are taking everything to the right of UXPV and then treating that value like a number.
您将一切都放在UXPV的右边,然后将该值当作数字对待。 But you no longer have a number for that value for one record.
但是,对于一条记录,您将不再有该值的数字。
So what you need to do is get rid of the bad record and change the user interface so that type of data cannot be added in the future. 因此,您需要做的是摆脱不良记录并更改用户界面,以便将来无法添加数据类型。 Or you need to fix the code to strip of all letter characters.
或者,您需要修复代码以去除所有字母字符。
I realized that all I had to do is add to ignore "SE" like this, 我意识到我要做的就是添加这样的代码来忽略“ SE”,
sqlAddOn = "and Name not like '%j%' and Name not like '%se%'"
sqlAddOnAudit = "and Server not like '%j%' and Server not like '%se%'"
Now it working and back to searching the way it suppose to be. 现在,它开始工作并返回到它应该是的搜索方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.