1

コントロール配列の作成テーブルの列名の取得に問題があります。出力された文字列をSQLクエリとして直接使用したので、文字列が機能することを知っています。問題は、の行が見つからないように見えるところにあります。テーブル(私が知っているのは、If lrd.HasRowsを使用して、行が見つからないことです( 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

'SelectWhere

 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 を知らずに言うのは難しいですが、1 つ以上のパラメーターが正しくない可能性があります。その機能をスキップして使用してみてください

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

クエリで count(*) を使用して、行数をテストすることもできます。

于 2012-11-28T21:39:31.740 に答える