|
1 | | -# How-to-display-data-from-Azure-SQL-Database-in-.NET-MAUI-DataGrid |
2 | | -How to display data from Azure SQL Database in .NET MAUI DataGrid? |
| 1 | +# How to display data from Azure SQL Database in .NET MAUI DataGrid ? |
| 2 | + |
| 3 | +Displaying data from an Azure SQL Database in a [.NET MAUI DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid) involves several steps: setting up your Azure SQL Database, connecting to it from your .NET MAUI application, and displaying the retrieved data in a DataGrid. Below is a step-by-step guide to help you achieve this. |
| 4 | + |
| 5 | + |
| 6 | +### Step 1: Set Up Azure SQL Database |
| 7 | + |
| 8 | +#### 1. Create an Azure SQL Database: |
| 9 | + |
| 10 | +* Log into the [Azure Portal](https://portal.azure.com/). |
| 11 | +* Navigate to "SQL Databases" and click "Add". |
| 12 | +* Fill in the required details (Database name, server, resource group, etc.) and create the database. |
| 13 | +* After creation, go to the database's "Connection strings" and copy the ADO.NET connection string for later use. |
| 14 | + |
| 15 | +#### 2 .Configure Firewall: |
| 16 | + |
| 17 | +* Go to your database settings in the Azure Portal. |
| 18 | +* Under "Firewalls and virtual networks", add your IP address to allow access to the database. |
| 19 | + |
| 20 | +### Step 2: Connect to Azure SQL Database |
| 21 | +* Install the [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient) package to interact with SQL Server |
| 22 | + |
| 23 | +* Create a Data Access Layer. |
| 24 | + |
| 25 | + |
| 26 | +##### C# |
| 27 | + |
| 28 | +Create a class for accessing the database and retrieving data. |
| 29 | +Use the ADO.NET connection string copied earlier. |
| 30 | + |
| 31 | +```C# |
| 32 | +public class DatabaseService |
| 33 | +{ |
| 34 | + public const string ConnectionString = "Your connection string here"; |
| 35 | + |
| 36 | + public IEnumerable<Stocks> PopulateData() |
| 37 | + { |
| 38 | + var items = new ObservableCollection<Stocks>(); |
| 39 | + |
| 40 | + using (SqlConnection conn = new SqlConnection(ConnectionString)) |
| 41 | + { |
| 42 | + conn.Open(); |
| 43 | + using (SqlCommand cmd = new SqlCommand("SELECT Ticker, CompanyName, LastPrice, Change, ChangePercent, OpenColumn,HighColumn,LowColumn,Volume,MarketCap FROM Stocks", conn)) |
| 44 | + { |
| 45 | + using (SqlDataReader reader = cmd.ExecuteReader()) |
| 46 | + { |
| 47 | + while (reader.Read()) |
| 48 | + { |
| 49 | + items.Add(new Stocks |
| 50 | + { |
| 51 | + Ticker = reader["Ticker"].ToString(), |
| 52 | + CompanyName = reader["CompanyName"].ToString(), |
| 53 | + LastPrice = Convert.ToDecimal(reader["LastPrice"]), |
| 54 | + Change = Convert.ToDecimal(reader["Change"]), |
| 55 | + ChangePercent = Convert.ToDecimal(reader["ChangePercent"]), |
| 56 | + OpenColumn = Convert.ToDecimal(reader["OpenColumn"]), |
| 57 | + HighColumn = Convert.ToDecimal(reader["HighColumn"]), |
| 58 | + LowColumn = Convert.ToDecimal(reader["LowColumn"]), |
| 59 | + Volume = Convert.ToInt64(reader["Volume"]), |
| 60 | + MarketCap = Convert.ToDecimal(reader["MarketCap"]) |
| 61 | + }); |
| 62 | + |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return items; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +``` |
| 73 | +##### ViewModel |
| 74 | + |
| 75 | +Populate the data in the OnAppearing() method. |
| 76 | + |
| 77 | +```C# |
| 78 | + public void InitialyzeAsync() |
| 79 | + { |
| 80 | + foreach (var item in _service.PopulateData()) |
| 81 | + { |
| 82 | + StockData.Add(item); |
| 83 | + } |
| 84 | + } |
| 85 | +``` |
| 86 | + |
| 87 | +#### XAML |
| 88 | +```XML |
| 89 | +<syncfusion:SfDataGrid x:Name="sfGrid" ColumnWidthMode="Auto" |
| 90 | + ItemsSource="{Binding StockData}"/> |
| 91 | +``` |
| 92 | + |
| 93 | +The following screenshot shows how to to display data from Azure SQL Database in .NET MAUI DataGrid. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-display-data-from-Azure-SQL-Database-in-.NET-MAUI-DataGrid) |
| 98 | + |
| 99 | +Take a moment to pursue this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. |
| 100 | +Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid(SfDataGrid). |
| 101 | + |
| 102 | +### Conclusion |
| 103 | +I hope you enjoyed learning about how to to display data from Azure SQL Database in .NET MAUI DataGrid. |
| 104 | + |
| 105 | +You can refer to our [.NET MAUI DataGrid's feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data. |
| 106 | +For current customers, you can check out our .NET MAUI components from the [License and Downloads](https://www.syncfusion.com/account/downloads) page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components. |
| 107 | +If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/account/login?ReturnUrl=%2Faccount%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dc54e52f3eb3cde0c3f20474f1bc179ed%26redirect_uri%3Dhttps%253A%252F%252Fsupport.syncfusion.com%252Fagent%252Flogincallback%26response_type%3Dcode%26scope%3Dopenid%2520profile%2520agent.api%2520integration.api%2520offline_access%2520kb.api%26state%3D8db41f98953a4d9ba40407b150ad4cf2%26code_challenge%3DvwHoT64z2h21eP_A9g7JWtr3vp3iPrvSjfh5hN5C7IE%26code_challenge_method%3DS256%26response_mode%3Dquery) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid). We are always happy to assist you! |
0 commit comments