Asp.net (web forms with VB.Net) connecting to sql database

I am trying to write a method in VB.net so that when I click a button, it queries the database and returns all the values ​​that correspond to the text box next to the button. I have no idea how to do this, I assume that in the onclick method for the button I will need to extract the value from the text box, connect to the database and display the results in gridview?

Any help is greatly appreciated.

thank:)

Mark

+3
source share
2 answers

The two “best” options are to either use a table adapter or an Entity Framework.

GUI . Entity Framework - . , , /, . (Que "easy" )

, , . , .

datagrid .

0

. SQL- . . :

strSQL = "SELECT * from Table where charindex ('" & TextBox1.Text & "', columnname) > 0 "

web.config , :

<connectionStrings>
<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />
</connectionStrings>

, - vb.net VisualStudio 2010.

Default.aspx - :

<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
<asp:Button runat="server" Text="Button" ID="Button1" />

- :

Imports System.Data.SqlClient

_Default    System.Web.UI.Page

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strSQL As String = String.Empty

    ' Define your select statement
    strSQL = "SELECT * from Table where charindex ('" & TextBox1.Text & "', columnname) > 0 "
    ' Fire up SQLConnection with a DataReader
    Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString)
        Dim command As New SqlCommand(strSQL, connection)
        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()

        While reader.Read()
            Try
                ' Do some magic with reader.GetValue()
            Catch ex As Exception
            End Try
        End While

        reader.Close()
        connection.Close()
    End Using
End Sub

, textbox.text , select, .

"CharIndex" , , , textbox.text, .

reader.GetValue .

SQLDataReader , , Grid- - ...

0

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


All Articles