How do I process the xamarin forms that connect to SQL Server and export data to the listView ? I'm looking for this simple example. What do I find on google ?
dongtrien 13 Newbie Poster
Dani AI
Generated
Short answer: do not connect a Xamarin.Forms app directly to SQL Server. Mobile clients should call a backend (e.g., ASP.NET Web API) that talks to SQL Server, returns JSON, and enforces auth/authorization. This avoids shipping DB credentials in the app, works over HTTPS, and scales better. From there, bind the JSON results to a ListView via an ObservableCollection.
Minimal client pattern you can drop in:
// Model
public record Account(string AccountNumber, string Name);
// API client
public sealed class ApiClient
{
private readonly HttpClient _http = new HttpClient { BaseAddress = new Uri("https://your-api.example.com/") };
public async Task<List<Account>> FindAccountsAsync(string number, CancellationToken ct = default)
{
using var resp = await _http.GetAsync($"api/accounts?number={WebUtility.UrlEncode(number)}", ct);
resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync(ct);
return await JsonSerializer.DeserializeAsync<List<Account>>(stream, cancellationToken: ct) ?? new();
}
}
// ViewModel
public sealed class AccountsViewModel : INotifyPropertyChanged
{
public ObservableCollection<Account> Items { get; } = new();
private readonly ApiClient _api = new();
public async Task LoadAsync(string number, CancellationToken ct = default)
{
Items.Clear();
foreach (var a in await _api.FindAccountsAsync(number, ct))
Items.Add(a);
}
public event PropertyChangedEventHandler PropertyChanged;
} Wire-up tips:
- Set
BindingContextto the view model and bindListView.ItemsSource="{Binding Items}". - Call
await vm.LoadAsync(searchNumber)inOnAppearing. - On the server, use parameterized queries/EF Core, enforce authentication, and paginate results.
- For large lists, prefer incremental loading and consider
CollectionViewinstead ofListViewfor better performance.
Rushabh Verma 0 Newbie Poster
Hi,
To connect to a SQL Server from a Xamarin app (using Visual Studio, so some instructions might change), I followed this process:
- Add in a System.Data reference to your project
- Add in a using directive "using System.Data.SqlClient"
- Define connection string and open the connection using SqlConnection(connectionString)
- Manipulate database as normal
My code connects to a SQL database located on a server (not localhost) and then searches for an account number to check if it exists. The code is:
string connectionString = @"Server=<ipAddress>;Database=<DBName>;User Id=<username>;Password=<password>;Trusted_Connection=true";
string databaseTable = "<yourDBTableName>";
string referenceAccountNumber = "0001134919";
string selectQuery = String.Format("SELECT * FROM {0} WHERE [Account_Number] = '{1}' ", databaseTable, referenceAccountNumber);
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//open connection
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
var result = command.ExecuteReader();
//check if account exists
var exists = result.HasRows;
}
}
catch (Exception exception)
{
#region connection error
AlertDialog.Builder connectionException = new AlertDialog.Builder(this);
connectionException.SetTitle("Connection Error");
connectionException.SetMessage(exception.ToString());
connectionException.SetNegativeButton("Return", delegate { });
connectionException.Create();
connectionException.Show();
#endregion
}` bngwebguru -17 Newbie Poster Banned
I've created a web service app (asp.net) that provides services to my Xamarin Forms apps so the mobile app will login to SQL Server via the web service and indeed perform all SQL actions (Select, Insert, Delete, etc) using the web service framework. I would suggest that's the way to go for you and/or welcome to cover in more detail my solution (which is generic SQL server forms access) in case that's of interest to you.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.