使用类在aspx vb中使用mysql表在下拉列表中填充数据

5w9g7ksd  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(365)

我试图加载一个下拉列表链接到一个表在mysql中的网页形式asp.net在vb中。我使用的类具有以下代码:

Public Class ClCountries
    Public Function Read(ByVal Opcion As Integer) As DataTable

        Dim cnn As New MySqlConnection(ConfigurationManager.ConnectionStrings("cnnPortalSmart").ConnectionString)
        Dim dt As New DataTable
        Try
            Dim cmd As New MySqlCommand("PA_COUNTRIES_SELECT", cnn)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@Opcion", Opcion)

            cnn.Open()
            dt.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges)
            cnn.Close()
        Catch ex As Exception
            Dim msgError As String
            msgError = ex.Message.ToString()
            cnn.Close()
        End Try
        Return dt
    End Function
End Class

它是通过以下方法从page\u load()调用的:

Sub LoadCountries()
    Try 
        Dim dv As New DataView(New ClCountries().Read(1)) 
        ddlCountry.DataSource = dv 
        ddlCountry.DataTextField = "Name" 
        ddlCountry.DataValueField = "ID" 
        ddlCountry.DataBind() 
        Dim li As New ListItem("Select Country", "0") 
        ddlCountry.Items.Insert(0, li) 
    Catch ex As Exception 
    End Try 
End Sub

aspx中的连接字符串:

<connectionStrings>    
    <add name="cnnPortalSmart" connectionString="Data Source=IP_NUMBER;Initial Catalog=SCHEMA_NAME; User ID=root;Password=XXXX;" providerName="MySql.Data.MySqlClient" />  
</connectionStrings>

谢谢您。

waxmsbnn

waxmsbnn1#

我通过将连接字符串修改为:

<connectionStrings>
<add name="cnnPortalSmart" connectionString="server=XXXX;user id=XXXX;password=XXXXX!;persistsecurityinfo=True;database=XXXXX;SslMode=none" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
1qczuiv0

1qczuiv02#

希望这对你有帮助

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT CustomerId, Name FROM Customers")
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                con.Open()
                ddlCustomers.DataSource = cmd.ExecuteReader()
                ddlCustomers.DataTextField = "Name"
                ddlCustomers.DataValueField = "CustomerId"
                ddlCustomers.DataBind()
                con.Close()
            End Using
        End Using
        ddlCustomers.Items.Insert(0, New ListItem("--Select Customer--", "0"))
    End If
End Sub

相关问题