2011-07-28 7 views
0

누구나 다음 작동하지 않는 이유를 알고 계십니까? 이 asp.net 코드가 작동하지 않습니다

Dim strPhrase As String = "'%office%'" 

objStringBuilder = New StringBuilder() 

objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString")) 

objSQLCommand = New SqlCommand("select col1, col2 from table1 where phrase like @phrase", objSQLConnection) 

objSQLCommand.Parameters.Add("@phrase", SqlDbType.VarChar, 255).Value = strPhrase 

objSQLCommand.Connection.Open() 
objSQLDataReader = objSQLCommand.ExecuteReader() 

While objSQLDataReader.Read() 
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2")) 
End While 

objSQLDataReader.Close() 
objSQLCommand.Connection.Close() 

return objStringBuilder.tostring() 

내가 phrase와 아무 제거하면

, 그것은 즉, 다시 작동하기 시작 :

objStringBuilder = New StringBuilder() 

objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString")) 

objSQLCommand = New SqlCommand("select col1, col2 from table1", objSQLConnection) 

objSQLCommand.Connection.Open() 
objSQLDataReader = objSQLCommand.ExecuteReader() 

While objSQLDataReader.Read() 
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2")) 
End While 

objSQLDataReader.Close() 
objSQLCommand.Connection.Close() 

return objStringBuilder.tostring() 

내가 오류를 얻을, 그냥 빈 문자열을 반환합니다.

답변

4

검색 어구에서 ''을 제거하면 명령 개체가 모든 것을 처리합니다. 즉

Dim strPhrase As String = "%office%" 
+0

+ 1, Four Figures Club에 오신 것을 환영합니다. D – Town

0

Dim strPhrase As String = "%office%" 

처럼 만들고 연결 문자열의 끝에 울부 짖는 라인을 추가 할 수 있습니다이

Dim strPhrase As String = "%office%" 
+0

oops its vb.net 그래서 당신은 필요 없어; – Mauro

+0

그것은 무엇입니까? VB.NET에 대해 전혀 몰라 :). 좋은 것을 알기 위해 – Rahul

1

처럼 만들

allow user variables=true; 
관련 문제