如何从vb.net查询中返回多个值
一个问题是如何将多个查询结果值存储或返回到多个变量中。。我使用的查询返回4列,但如何。。单独将这些结果存储到4个单独的变量中。。这是我的密码 私有子FrmAlumnos_LoadByVal发送方作为System.Object,ByVal e作为System.EventArgs处理MyBase.Load txtCurrentUser.Text=Login.txtexser.Text如何从vb.net查询中返回多个值,vb.net,postgresql,Vb.net,Postgresql,一个问题是如何将多个查询结果值存储或返回到多个变量中。。我使用的查询返回4列,但如何。。单独将这些结果存储到4个单独的变量中。。这是我的密码 私有子FrmAlumnos_LoadByVal发送方作为System.Object,ByVal e作为System.EventArgs处理MyBase.Load txtCurrentUser.Text=Login.txtexser.Text Dim strsql As String strsql = "SELECT ""Agregar"",
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只是为了让您知道…我想您可能会发现这里有一个有用的数据集。比如:
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")
希望有帮助
-科幻小说
编辑:经过更多的搜索,我找到了链接,这可能会帮助你!这是postgreSQL特有的
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中循环。您需要使用DataReader的读取方法:
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注入攻击。为什么不进行4次查询?是的,我知道我有4次查询,每次查询一次,但是否有一种方法可以在一次查询中完成?reply1查询的thnx=1对象。因此,如果您只需要一个查询,您可以创建一个对象,然后将该对象拆分为4个新对象。ExecuteReader返回一个数据读取器。但是我如何比较第一个元素、第二个元素和第三个元素……如果postgreSQL不支持这种语法,那么我想它不会有多大用处。也许这会引导你做类似的事情!但是当调用ExecuteReader方法时,仍然向数据库发送4个查询!是的,你说得对。我的意思是将comando.ExecuteReader.Item0=0的行更改为registro0=0或您要查找的任何字段。基本上…一旦运行ExecuteReader,只需使用reader对象寄存器,而不是在运行之后调用ExecuteReader 4次。希望这是有意义的。当然,我的假设是你返回了不止一张记录。