如何从查询vb.net返回多个值

时间:2012-04-10 14:53:01

标签: vb.net postgresql

一个问题如何将多个查询结果值存储或返回到多个变量中..我正在使用一个返回4列的查询,但如何...将这些结果存储到4个单独的变量中..这是我的代码

Private Sub FrmAlumnos_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load         txtCurrentUser.Text = Login.txtUser.Text

    Dim strsql As String
    strsql = "SELECT ""Agregar"", ""Modificar"", ""Eliminar"", ""Imprimir"" FROM ""Seguridad"".""GrupoPantallas"" WHERE ""IdGrupo"" = (SELECT ""IdGrupo"" FROM ""Seguridad"".""Users"" WHERE ""IdUsers"" = '" _
        + Me.txtCurrentUser.Text + "') AND ""IdPantalla"" = '" + Me.Name + "'"
    Try
        Using conexion As New Devart.Data.PostgreSql.PgSqlConnection(My.Settings.CNX_Principal)
            Dim comando As New Devart.Data.PostgreSql.PgSqlCommand(strsql, conexion)
            conexion.Open()
            Dim registro As Devart.Data.PostgreSql.PgSqlDataReader = comando.ExecuteReader
            If comando.ExecuteReader.Item(0) = 0 Then
                btnNew.Visible = False
            End If
            If comando.ExecuteReader.Item(1) = 0 Then
                btnEdit.Visible = False
            End If
            If comando.ExecuteReader.Item(2) = 0 Then
                btnDelete.Visible = False
            End If
            If comando.ExecuteReader.Item(3) = 0 Then
                btnPrint.Visible = False
            End If
        End Using

    Catch ex As Exception

    End Try
End Sub

我正在使用PostgreSQL只是为了让你知道...

3 个答案:

答案 0 :(得分:0)

我认为你可能会发现DataSet在这里很有用。类似的东西:

Dim ds As New DataSet
Dim com As New SqlCommand
com.Connection = <yourconnectionstring>
com.CommandType = CommandType.Text
com.CommandText = "YOURSQLSTUFF"
Dim da As New DataAdapter
da.SelectCommand = com

da.Fill(ds)
ds.Tables(0).TableName = "FirstTable"
ds.Tables(0).PrimaryKey = New DataColumn() {ds.Tables(0).Columns("primaryKeyOfFirstTable")
ds.Tables(1).TableName = "SecondTable"
ds.Tables(1).PrimaryKey = New DataColumn() {ds.Tables(1).Columns("primaryKeyOfSecondTable")

希望有所帮助!

-sf

编辑:经过一番搜索后,我找到了this链接,这可能对您有所帮助!它是特定于postgreSQL的!

答案 1 :(得分:0)

您需要使用DataReader的Read方法:

Dim strsql As String
strsql = "SELECT ""Agregar"", ""Modificar"", ""Eliminar"", ""Imprimir"" FROM ""Seguridad"".""GrupoPantallas"" WHERE ""IdGrupo"" = (SELECT ""IdGrupo"" FROM ""Seguridad"".""Users"" WHERE ""IdUsers"" = '" _
    + Me.txtCurrentUser.Text + "') AND ""IdPantalla"" = '" + Me.Name + "'"
Try
    Using conexion As New Devart.Data.PostgreSql.PgSqlConnection(My.Settings.CNX_Principal)
        Dim comando As New Devart.Data.PostgreSql.PgSqlCommand(strsql, conexion)
        conexion.Open()
        Using registro As Devart.Data.PostgreSql.PgSqlDataReader = comando.ExecuteReader()
            //Assuming that there is only a single row returned
            If registro.Read()
                btnNew.Visible = registro.GetBoolean(0)
                btnEdit.Visible = registro.GetBoolean(1)
                btnDelete.Visible = registro.GetBoolean(2)
                btnPrint.Visible = registro.GetBoolean(3)
            End While
        End Using
    End Using
Catch ex As Exception

End Try

您还应该考虑使用参数。它会使代码比使用连接字符串更清晰,并且会阻止sql注入攻击。

答案 2 :(得分:-1)

Dim strsql As String
strsql = "SELECT ""Agregar"", ""Modificar"", ""Eliminar"", ""Imprimir"" FROM ""Seguridad"".""GrupoPantallas"" WHERE ""IdGrupo"" = (SELECT ""IdGrupo"" FROM ""Seguridad"".""Users"" WHERE ""IdUsers"" = '" _
    + Me.txtCurrentUser.Text + "') AND ""IdPantalla"" = '" + Me.Name + "'"
Try
    Using conexion As New Devart.Data.PostgreSql.PgSqlConnection(My.Settings.CNX_Principal)
        Dim comando As New Devart.Data.PostgreSql.PgSqlCommand(strsql, conexion)
        conexion.Open()
        Dim registro As Devart.Data.PostgreSql.PgSqlDataReader = comando.ExecuteReader
        //This is the loop that you missed
        While registro.Read()
            If comando.ExecuteReader.Item(0) = 0 Then
                btnNew.Visible = False
            End If
            If comando.ExecuteReader.Item(1) = 0 Then
                btnEdit.Visible = False
            End If
            If comando.ExecuteReader.Item(2) = 0 Then
                btnDelete.Visible = False
            End If
            If comando.ExecuteReader.Item(3) = 0 Then
                btnPrint.Visible = False
            End If
        End While
    End Using

Catch ex As Exception

End Try

我不确定这是否是你要做的,但你所要做的就是循环遍历DataReader,如上所示。