1

로 SQL 조건을 전달합니다내가 다음 코드를 가지고은 OleDb 매개 변수

Dim executedCmd As OleDb.OleDbCommand = m_dbMgr.GetCommand() 
executedCmd.CommandText = "select * from [Parameters] where " 
Dim SQLcondition As String = String.Empty 
For i As Integer = 0 To ParameterName.Count - 1 
executedCmd.CommandText += "ParameterName = @parametername" + i.ToString() + " and ParameterValue @ParamaterCondition" + i.ToString() + " @ParameterValue" + i.ToString() 
executedCmd.Parameters.AddWithValue("@parametername" + i.ToString(), ParameterName(i)) 
executedCmd.Parameters.AddWithValue("@ParameterValue" + i.ToString(), ParameterValue(i)) 
executedCmd.Parameters.AddWithValue("@ParamaterCondition" + i.ToString(), Condition(i)) 
Next 

ParameterName, ParameterValue, ParameterCondition 모두 같은 길이 ArrayList하지만 코드가 제대로 작동하지 않습니다. 모든 변수에 값이 있는지 확인했습니다.

나는 그것이 구문 오류를보고 코드를 실행하면 "쿼리 식에없는 작업"

문제는 ParameterCondition 같은 값을 가지고 있다는 것입니다을 ('>', '<', '=', .... 어떤 논리적 SQL 사업자) .

편집 : 매개 변수에 조건을 포함하려면 어떻게해야합니까?

답변

0

데이터베이스 엔진은 매개 변수를 값으로 바꾸기 전에 SQL 문의 구문을 검사합니다. 그래서 "WHERE @ p1 @ p2 @ p3"같은 구문은 올바르게 구문 분석되지 않습니다. 조건을 문자열 표현식으로 바꿉니다.

+0

해당 조건을 매개 변수화하는 방법은 무엇입니까? 방법이 있니? – Nest

+0

코드가 업데이트되었습니다. 이제는 더 명확 해지기를 바랍니다. –

1

1 = 1을 추가하면 논리적 표현을 단순화 할 수 있습니다. AND 논리 연산자로 추가 된 모든 조건에 유의하십시오. 열 이름에서 매개 변수 이름을 생성 할 수 있습니다.

executedCmd.CommandText = "select * from [Parameters] where 1 = 1" 

.... 
executedCmd.CommandText += " AND " + ParameterName(i) + " " + Condition(i) + " @" + ParameterName(i) 
executedCmd.Parameters.AddWithValue("@" + ParameterName(i), ParameterValue(i)) 

ParameterCondition은 모두 2 진수 연산자 여야합니다.

관련 문제