1

我在创建控制数组获取表的列名时遇到问题,我知道我的字符串可以正常工作,因为我直接将输出的字符串用作 SQL 查询,问题在于它似乎找不到任何表(我知道是他们的,使用If lrd.HasRows Then 我已经看到它没有找到任何行(lrd.HasRows = False)。它们是INFORMATION_SCHEMA.COLUMNS的不同连接字符串吗?

'查找列名

Public Sub findSQLColumnName(ByRef i As Integer, ByRef OutputValue As String, ByVal tableName As String)
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim lrd As SqlDataReader

    Dim TableNameParm As New SqlParameter("Tablename", tableName) 'adds in the new paramenter UserName
    TableNameParm.Direction = ParameterDirection.Output
    Dim LocationParm As New SqlParameter("Location", i) 'adds in the new paramenter UserName
    LocationParm.Direction = ParameterDirection.Input

    Call FindConnectionString(con) ' finds connection string

    cmd.Parameters.Add(TableNameParm)
    cmd.Parameters.Add(LocationParm)

    Call SQLSELECT_WHERE("INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME AS Output, ORDINAL_POSITION", True, " (TABLE_NAME = @Tablename) AND (ORDINAL_POSITION = @Location)", con, cmd, lrd)

    Try
        ' While lrd.Read()    ' code writen within here for what is to be done with selected data.
        'Call findSQLColumnValue("Output", lrd, OutputValue)
        'End While

        If lrd.HasRows Then
            lrd.Read()
            Call findSQLColumnValue("Output", lrd, OutputValue)
            lrd.Close()
            'Close connection before Redirecting.
        Else
            lrd.Close()

        End If

        '    Catch ex As Exception
    Finally
        con.Close()
    End Try

End Sub

'查找列的值

Public Sub findSQLColumnValue(ByRef ColumnName As String, loader As SqlDataReader, ByRef OutputValue As String)
    OutputValue = (Convert.ToString(loader(ColumnName))).Trim
End Sub

'按钮单击(创建控件数组)

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim SQLCode As New SQLCode
    Dim TableLength As Integer
    Dim lblText(100) As String
    Call SQLCode.SQLFindNoColumns("PatientClinicalinformation", TableLength, lblTitlePatient, lblText)
    For i As Int16 = 1 To TableLength
        ' Create the label control and set its text attribute
        Dim Label2 As New Label
        Call SQLCode.findSQLColumnName(i.ToString, lblText(i), "PatientClinicalinformation")
        Label2.Text = lblText(i)

        Dim Literal2 As New Literal
        Literal2.Text = "<br />"

        ' Add the control to the placeholder
        PlaceHolder1.Controls.Add(Label2)
        Label2.ID = "lbl" & i
        PlaceHolder1.Controls.Add(Literal2)
    Next

End Sub

'选择地点

 Public Sub SQLSELECT_WHERE(ByVal Tables As String, ByVal Columns As String, ByVal WHERE As Boolean, ByVal WHEREStatement As String, ByRef connection As SqlConnection, ByRef command As SqlCommand, ByRef loader As SqlDataReader)
    connection.Open()
    command.Connection = connection
    If WHERE = False Then
        command.CommandText = " SELECT " & Columns & " FROM " & Tables
    End If
    If WHERE = True Then
        command.CommandText = " SELECT " & Columns & " FROM " & Tables & " WHERE " & WHEREStatement
    End If
    command.CommandText = command.CommandText
    loader = command.ExecuteReader()
End Sub
4

2 回答 2

1

我找到了解决方案!代码一切正常,数组 TableNameParm 有问题

   Dim TableNameParm As New SqlParameter("Tablename", tableName) 'adds in the new paramenter UserName
    TableNameParm.Direction = ParameterDirection.Output
    Dim LocationParm As New SqlParameter("Location", i) 'adds in the new paramenter UserName
    LocationParm.Direction = ParameterDirection.Input

TableNameParm.Direction 应该是输入,但设置为输出

       Dim TableNameParm As New SqlParameter("Tablename", tableName) 'adds in the new paramenter UserName
    TableNameParm.Direction = ParameterDirection.Input
    Dim LocationParm As New SqlParameter("Location", i) 'adds in the new paramenter UserName
    LocationParm.Direction = ParameterDirection.Input
于 2012-11-30T09:59:22.660 回答
0

不知道函数 SQLSELECT_WHERE 很难说,但有可能一个或多个参数不正确。尝试跳过该功能并使用

cmd = New SqlCommand("SELECT ... WHERE", conn)

您还可以通过在查询中使用 count(*) 来测试行数。

于 2012-11-28T21:39:31.740 回答