【问题标题】:This asp.net code is not working这个 asp.net 代码不起作用
【发布时间】:2011-10-15 03:13:54
【问题描述】:

任何人都知道为什么以下不起作用?

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()

我没有收到任何错误,它只是返回一个空白字符串。

【问题讨论】:

    标签: .net asp.net vb.net .net-3.5 asp.net-3.5


    【解决方案1】:

    从您的搜索短语中删除“”,命令对象会为您处理所有这些。即

    Dim strPhrase As String = "%office%"
    

    【讨论】:

      【解决方案2】:

      变成这样

      Dim strPhrase As String = "%office%"
      

      【讨论】:

      • oops 它的 vb.net 所以你不需要 ;
      • 是吗?不知道 VB.NET :)。不过很高兴知道
      【解决方案3】:

      你可以这样做

      Dim strPhrase As String = "%office%"
      

      并在连接字符串的末尾添加波纹管

      allow user variables=true;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-05
        • 2012-02-22
        • 2023-03-03
        • 2017-10-02
        • 2016-07-10
        • 2010-12-14
        • 2017-04-09
        • 2013-01-30
        相关资源
        最近更新 更多