SQL selects the return value of a variable

works with: ASP.net using VB.net connection to MS SQL Server

What I'm trying to do is take the result of the SQL select query and put it in a string variable so that it can be used in things like a text box or shortcut. the code doesn't work yet ...

Imports System.Data.SqlClient

Partial class dev_Default Inherits System.Web.UI.Page

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load


    Dim cnPodaci As New SqlConnection
    cnPodaci.ConnectionString = "Data Source=<server>;Initial Catalog=<DB>;User ID=<UserName>;Password=<Password>"
    cnPodaci.Open()
    Dim cm As New SqlCommand
    cm.CommandText = "SELECT * FROM tbl1"
    cm.Connection = cnPodaci
    Dim dr As SqlDataReader
    dr = cm.ExecuteReader

    TextBox1.Text = dr.GetString(0)


    cnPodaci.Close()

End Sub

Final class

+3
source share
3 answers

, , "ExecuteReader" , (DataReader), . "" DataReader ( while). - :

If dr.Read() Then
    TextBox1.Text = dr.GetString(0)
End If

, , , , , .

+4

, ExecuteScalar:

 TextBox1.Text = DirectCast(cm.ExecuteScalar(), String)
+2

, SELECT , , , .

? , , ?

-2
source

Source: https://habr.com/ru/post/1717644/


All Articles