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、フォーフィギュアクラブへようこそ:D – Town

0

あなたはこの

Dim strPhrase As String = "%office%" 

のようにそれを作ると、あなたの接続文字列の末尾に怒鳴るの行を追加することができ、この

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

oops vb.netので、必要はありません。 – Mauro

+0

それは? VB.NETについてのアイデアは:)。良いことを知りたい – Rahul

1

ようにそれを作る

allow user variables=true; 
関連する問題