Using ExecuteReader in MySQL in ASP.Net

Last Reply 15 days ago By dharmendr

Posted 15 days ago

Can the script to display data on SQL Server be implemented in MySQL?

Imports System.Web
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text
Imports System.Collections.Generic
Imports System.Web.Script.Serialization

Public Class cariAHASS
    Implements System.Web.IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim prefixText As String = context.Request.QueryString("term")
        Using conn As New SqlConnection()
            conn.ConnectionString = ConfigurationManager.ConnectionStrings("conn").ConnectionString
            Using cmd As New SqlCommand()
                cmd.CommandText = "select description from m_cabang_ahass where kota like '%'+ @SearchText + '%'"
                cmd.Parameters.AddWithValue("@SearchText", prefixText)
                cmd.Connection = conn
                Dim customers As New List(Of String)()
                conn.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        customers.Add(sdr("description").ToString())
                    End While
                End Using
                conn.Close()
                context.Response.Write(New JavaScriptSerializer().Serialize(customers))
            End Using
        End Using
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

 

Posted 15 days ago
arie.keren says:
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
 
        Dim prefixText As String = context.Request.QueryString("term")
        Using conn As New SqlConnection()
            conn.ConnectionString = ConfigurationManager.ConnectionStrings("conn").ConnectionString
            Using cmd As New SqlCommand()
                cmd.CommandText = "select description from m_cabang_ahass where kota like '%'+ @SearchText + '%'"
                cmd.Parameters.AddWithValue("@SearchText", prefixText)
                cmd.Connection = conn
                Dim customers As New List(Of String)()
                conn.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        customers.Add(sdr("description").ToString())
                    End While
                End Using
                conn.Close()
                context.Response.Write(New JavaScriptSerializer().Serialize(customers))
            End Using
        End Using
    End Sub

Yes. You can implement in MySQL. Replace the code with the below.

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim prefixText As String = context.Request.QueryString("term")
    Using conn As New MySqlConnection()
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Using cmd As New MySqlCommand()
            cmd.CommandText = "select description from m_cabang_ahass where kota like '%'+ @SearchText + '%'"
            cmd.Parameters.AddWithValue("@SearchText", prefixText)
            cmd.Connection = conn
            Dim customers As New List(Of String)()
            conn.Open()
            Using sdr As MySqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    customers.Add(sdr("description").ToString())
                End While
            End Using
            conn.Close()
            context.Response.Write(New JavaScriptSerializer().Serialize(customers))
        End Using
    End Using
End Sub

Refer below article for using ExecuteReader in MySQL.

Using MySql Database with MySql Connector in ASP.Net MVC Razor Tutorial with example