Home
Search
SqlDataAdapter ExampleUse SqlDataAdapter with SQL to populate a DataTable for use in Windows Forms.
C#
This page was last reviewed on Sep 29, 2022.
SqlDataAdapter. This C# class interacts with the DataTable type. It can be used to fill a DataTable with a table from your SQL Server database.
Type notes. We review important members (methods, events and properties) on SqlDataAdapter. We should include the System.Data namespace to access this type.
Example. You can change the SELECT command to use a table from a custom database. You may also need to add a DataGridView to the Windows Forms program.
DataGridView
Note SqlDataAdapter doesn't require the DataGridView. However, DataGridViews are a common usage.
Part 1 This creates a new SqlConnection instance. Note that you must include the System.Data.SqlClient namespace in your program.
Part 2 We use another using block. The using statements are ideal for disposing of resources, making programs more efficient and reliable.
SqlClient
Part 3 We instantiate and Fill a new DataTable. Fill() will populate the internal rows and columns of the DataTable to match the SQL result.
Part 4 The DataSource is assigned to the DataTable. The result will be a filled DataGridView with data from SQL Server.
DataSource
using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); FillData(); } void FillData() { // Part 1: open connection. using (SqlConnection c = new SqlConnection( Properties.Settings.Default.DataConnectionString)) { c.Open(); // Part 2: create new DataAdapter. using (SqlDataAdapter a = new SqlDataAdapter( "SELECT * FROM EmployeeIDs", c)) { // Part 3: Use DataAdapter to fill DataTable. DataTable t = new DataTable(); a.Fill(t); // Part 4: render data onto the screen. // dataGridView1.DataSource = t; } } } } }
Fill. Fill is the most important method on the DataAdapter objects. It executes the query and fills the DataAdapter object with the results.
OnFillError This is an event that allows you to listen to when an error occurred when filling the DataTable.
GetFillParameters This allows you to get the parameters that are being used in a SELECT statement.
Options. LoadOption is an enumeration of the options you can use when the DataAdapter is loaded. You can specify OverwriteChanges, PreserveChanges and Upsert.
AcceptChangesDuringUpdate You code can use the AcceptChanges method when you want to accept all changes made to the row.
OnRowUpdated, OnRowUpdating. These events allow you to receive messages when the specified actions occur. They are useful when you must be alerted to changes.
SqlCommandBuilder. SqlDataAdapater can push even more of the error-prone coding into the ADO.NET methods when you also use SqlCommandBuilder.
SqlCommandBuilder
A summary. We used SqlDataAdapter, part of the System.Data.SqlClient namespace. There are many members of SqlDataAdapter. We touched on SqlCeDataAdapter, which is similar.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 29, 2022 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.