SQL 사용에 대해서는 잊어 버리십시오. 데이터베이스 서버에서 데이터를 사용, 액세스 및 수정하는 것이 그 역할입니다.
VB.NET에서는 Left
, Mid
, Right
및 InStr
함수를 사용하여 SQL 문자열을 구문 분석 할 수 있습니다.
' Using your string : UPDATE mytable SET aaa='test',bbb=123 WHERE id=508
strWithoutFirstPart = Mid(strSQL, InStr(strSQL, "SET")) ' Output : SET aaa='test',bbb=123 WHERE id=508
strWithoutLastPart = Left(strWithoutFirstPart, InStr(strWithoutFirstPart, "WHERE")-1) ' OutPut : SET aaa='test',bbb=123
This has more information on the functions.
'Global variables
Dim parameters, values
parameters = Array()
values = Array()
' Procedure for parsing an Update SQL Statement to get parameters and values.
Sub parseSQLUpdate(ByVal strSQL)
' Variables
Dim strParsed, intEqualPos, intCommaPos, intLastPos, regExStrCommaTest
' Clearing the global variables.
ReDim parameters(0), values(0)
' Parsing the string for the right section.
strParsed = Trim(Mid(strSQL, InStr(UCase(strSQL), "SET") + 4))
strParsed = Trim(Left(strParsed, InStr(UCase(strParsed), "WHERE") - 2))
' Getting positions.
intEqualPos = InStr(strParsed, "=")
intCommaPos = InStr(strParsed, ",")
intLastPos = 0
' Preparing the Reg. Ex. variable
Set regExStrCommaTest = new RegExp
regExStrCommaTest.Pattern = "('[^']*'$)|(^[^']+$)"
' Looping for every parameter/value.
Do While (intEqualPos > 0)
' Check if first loop.
If (intLastPos = 0) Then
parameters(UBound(parameters)) = Trim(Left(strParsed, intEqualPos - 1))
' Check if at last parameter/value.
If (intCommaPos > 0) Then
' Check for commas in text.
Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
' Go to next comma.
intCommaPos = InStr(intCommaPos + 1, strParsed, ",")
' Already at end, so take remaining characters.
If (intCommaPos <= 0) Then
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
Exit Sub
End If
Loop
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
Else
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
End If
Else
' Change arrays' size.
ReDim Preserve parameters(UBound(parameters) + 1), values(UBound(values) + 1)
parameters(UBound(parameters)) = Trim(Mid(strParsed, intLastPos + 1, intEqualPos - intLastPos - 1))
' Check if at last parameter/value.
If (intCommaPos > 0) Then
' Check for commas in text.
Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
' Go to next comma.
intCommaPos = InStr(intCommaPos + 1, strParsed, ",")
' Already at end, so take remaining characters.
If (intCommaPos <= 0) Then
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
Exit Sub
End If
Loop
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
Else
values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
End If
End If
' Remembering last position
intLastPos = intCommaPos
' Ending Loop if last position is 0
If (intLastPos = 0) Then
Exit Sub
End If
' Getting new positions.
intEqualPos = InStr(intLastPos, strParsed, "=")
intCommaPos = InStr(intEqualPos, strParsed, ",")
Loop
End Sub
와우 ,, 감사합니다. 나는 그것을 바로 시도 할 것이다 :) –
정말 잘 작동 :) 다시 한번 감사드립니다. –