SQLClient命令参数查询字符串长度

时间:2018-03-22 19:23:46

标签: string vb.net sqlclient

我正在连接到SQL Server,并试图通过添加参数来限制结果。我添加的第一个参数@sdate工作正常。但是,现在我正在尝试添加第二个不起作用的参数。我希望只有在字符串的长度大于6个字符的长度时才返回字段LP_EOC_DATA.PL。下面执行的代码,就像我说的,返回的日期是正确的,但它也返回了LP_EOC_DATA.PL中的字符串长度小于6的值。如果你知道如何使它工作,请告诉我。提前谢谢。

Sub doSQL()
    Dim myConn As SqlConnection
    Dim myCmd As SqlCommand
    Dim myReader As SqlDataReader
    Dim sqlString As String = "SELECT LP_EOC_DATA.PL as PLs, LP_EOC_DATA.cDate as ReadDate, LP_EOC_LOV.LOCATION as Location " &
        "FROM LP_EOC_DATA INNER JOIN LP_EOC_LOV ON LP_EOC_DATA.PIC = LP_EOC_LOV.PIC " &
        "WHERE LP_EOC_DATA.cDate > (@sdate) AND LEN(LP_EOC_DATA.PL) > @slen1 " &
        "UNION SELECT dbo.VT_DATA.PL as PLs, dbo.VT_DATA.cDate as ReadDate, dbo.VT_LOV.LOCATION as Location " &
        "FROM dbo.VT_DATA INNER JOIN dbo.VT_LOV ON dbo.VT_DATA.PIC = dbo.VT_LOV.PIC " &
        "WHERE dbo.VT_DATA.cDate > (@sdate) AND LEN(dbo.VT_DATA.PL) > @slen1 " &
        "ORDER BY ReadDate;"

    myConn = New SqlConnection("SERVER=ServerName;UID=uName;" &
                            "PWD=Password;")
    myCmd = myConn.CreateCommand
    myCmd.CommandText = sqlString
    myCmd.Parameters.AddWithValue("@sdate", DateTimePicker1.Value)
    myCmd.Parameters.AddWithValue("@slen1", 6)
    'myCmd.Parameters.AddWithValue("@rx1", "'%[^0-9a-z]%'")
    'myCmd.Parameters.AddWithValue("@rx2", " dbo.VT_DATA.PL NOT LIKE '%[^0-9a-z]%'")

    myConn.Open()
    myReader = myCmd.ExecuteReader()
    Table.Load(myReader)

    DataGridView1.Visible = True
    DataGridView1.DataSource = Table
    lblTotal.Text = Table.Rows.Count
End Sub

另外,正如您所看到的,我希望添加另一个参数,该参数仅返回来自相同LP_EOC_DATA.PL字段的字母数字结果。我还没有那么远,但如果你看到我在那里做错了什么,我也很感激你的投入。

2 个答案:

答案 0 :(得分:0)

如果您将SQL格式化一点,这会有所帮助。有一些结构,但它仍然作为一个大墙文本。调试对你来说比你更难,因为我们根本不了解你的架构。在我们解决问题之前还应该做一些其他的小事情(Using阻止在异常的情况下关闭连接,避免AddWithValue()用于索引安全,从用户界面隔离SQL等等):

Function doSQL(StartDate As DateTime) As DataTable
    Dim result As New DataTable
    Dim sqlString As String = _
        "SELECT LP_EOC_DATA.PL as PLs, LP_EOC_DATA.cDate as LPRReadDate, LP_EOC_LOV.LOCATION as Location " & 
        "FROM LP_EOC_DATA " & 
        "INNER JOIN LP_EOC_LOV ON LP_EOC_DATA.PIC = LP_EOC_LOV.PIC " & 
        "WHERE LP_EOC_DATA.cDate > @sdate AND LEN(COALESCE(LP_EOC_DATA.PL,'')) > @slen1 " &
        "UNION " & 
        "SELECT dbo.VT_DATA.PL as PLs, dbo.VT_DATA.cDate as ReadDate, dbo.VT_LOV.LOCATION as LPRLocation " &
        "FROM dbo.VT_DATA " &
        "INNER JOIN dbo.VT_LOV ON dbo.VT_DATA.PIC = dbo.VT_LOV.PIC " &
        "WHERE dbo.VT_DATA.cDate > @sdate AND LEN(COALESCE(dbo.VT_DATA.PL,'')) > @slen1 " &
        "ORDER BY ReadDate;"

    Using myConn As New SqlConnection("SERVER=ServerName;UID=uName;" &
                            "PWD=Password;"), _
          myCmd As New SqlCommand(sqlString, myConn)

        myCmd.Parameters.Add("@sdate", SqlDbType.DateTime).Value = StarDate
        myCmd.Parameters.Add("@slen1", SqlDbType.Int).Value = 6

        myConn.Open()
        result.Load(myCmd.ExecuteReader())
    End Using

    Return result
End Function

然后像这样称呼它:

Dim tbl As DataTable = doSql(DateTimePicker1.Value)
DataGridView1.Visible = True
DataGridView1.DataSource = tbl
lblTotal.Text = tbl.Rows.Count

至于问题,有几种可能性:NULL值可以在这种情况下产生意外结果(我发布的代码已经说明了这一点)。您可能也遇到某些unicode空格填充字符数的麻烦。另一种可能性是charnchar字段,而不是varcharnvarchar,但我不认为这是问题所在。

答案 1 :(得分:0)

这不是问题本身的答案,而是对XML文字示例请求的回复。由于这需要几行代码,我宁愿不把它放在评论中。

Dim sql = <sql>
              SELECT *
              FROM MyTable
              WHERE MyColumn = @MyColumn
          </sql>
Dim command As New SqlCommand(sql.Value, connection)

请注意,元素名称可以是您想要的任何内容,但我通常使用&#39; sql&#39;什么时候用于SQL代码。