need to get value from sql query

前端 未结 4 2028
一向
一向 2020-12-20 08:28

Through ADO,I like to do the following query:

    select name, address, zip from terr where id = \'33334\'

I like then to assign name, adde

相关标签:
4条回答
  • 2020-12-20 09:02

    Try somethign like this:

      Dim dbName As String
      Dim dbAddress As String
      Dim dbZip As String
      Using connObj As New SqlClient.SqlConnection("<connectionString>")
         Using cmdObj As New SqlClient.SqlCommand("select name, address, zip from terr where id = '33334'", connObj)
            connObj.Open()
            Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
               'This will loop through all returned records 
               While readerObj.Read
                  dbName = readerObj("name").ToString
                  dbAddress = readerObj("address").ToString
                  dbZip = readerObj("zip").ToString
                  'handle returned value before next loop here
               End While
            End Using
            connObj.Close()
         End Using
      End Using
    

    Also, you should look into parameterizing the value for the where clause.

    0 讨论(0)
  • 2020-12-20 09:04

    execute a SqlCommand from SQLDatareader, like:

    Dim vVendedor As New SqlCommand("SELECT user FROM users", mConeccion)
    vDatosVen = vVendedor.ExecuteReader
    vVendedor = Nothing
    

    and to get te value:

    While vDatosVen.Read() 
       vUser = vDatosVen("user")
    End While
    
    0 讨论(0)
  • 2020-12-20 09:13

    Here's what i did...

       Private Sub btn_Connect_Click(sender As Object, e As EventArgs) Handles btn_Connect.Click
        Dim sql_connection As New MySqlConnection
        Dim sql_query As New MySqlCommand
        Dim sql_result As MySqlDataReader
        sql_connection.ConnectionString = "Server=localhost;Database=hidden;Uid=root;Pwd=;"
        sql_query.Connection = sql_connection
        sql_connection.Open()
        sql_query.CommandText = "SELECT Entry,name FROM table WHERE entry=1;"
        sql_result = sql_query.ExecuteReader
        If sql_result.HasRows Then
    
            Do While sql_result.Read()
                Dim query_result As String
                query_result = sql_result("name")
                MsgBox(query_result)
            Loop
        Else
            MsgBox("No results found.")
        End If
    
    0 讨论(0)
  • 2020-12-20 09:15

    You need a DataBase (i assume MS Sql-Server), a Connection and a DataAdapter to fill a DataTable. Then you have all you need. Here's an example:

    Public Function GetUser(UserId As Int32) As DataRow
        Using con = New SqlConnection(My.Settings.RM2ConnectionString)
            Using cmd = New SqlCommand("select name, address, zip from terr where id = @id", con)
                cmd.Parameters.AddWithValue("@id", UserId)
                Dim da = New SqlDataAdapter(cmd)
                Dim tblUser = New DataTable
                da.Fill(tblUser)
                If tblUser.Rows.Count <> 0 Then
                    Return tblUser(0)
                Else
                    Return Nothing
                End If
            End Using
        End Using
    End Function
    
    0 讨论(0)
提交回复
热议问题