using System; using System.Collections.Generic; using System.Threading.Tasks; using MySql.Data.MySqlClient; using Xamarin.Forms; namespace myApp { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); GetDataFromDatabase(); } async void GetDataFromDatabase() { string connectionString = Server=yourserver;Database=yourdatabase; Uid=yourusername;Pwd=yourpassword;"; using (MySqlConnection connection = new MySqlConnection(connectionString)) { try { await connection.OpenAsync(); string query = "SELECT YourColumnName FROM YourTable"; using (MySqlCommand cmd = new MySqlCommand(query, connection)) using (MySqlDataReader reader = cmd.ExecuteReader()) { List<YourDataModel> data = new List<YourDataModel>(); while (await reader.ReadAsync()) { data.Add(new YourDataModel { YourColumnName = reader["YourColumnName"].ToString() }); } dataListView.ItemsSource = data; } } catch (Exception ex) { // Handle any exceptions Console.WriteLine(ex.Message); } } } } public class YourDataModel { public string YourColumnName { get; set; } } }
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="App10.MainPage"> <StackLayout> <Label Text="Welcome" TextColor="Black"/> <ListView x:Name="dataListView"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding YourColumnName}" TextColor="Black"/> </DataTemplate> </ListView.ItemTemplate> </ListView> <Label Text="Work label after table binding" TextColor="Blue" /> </StackLayout> </ContentPage>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)