Home > Backend Development > C++ > How to Populate a C# DataTable from a SQL Server Table?

How to Populate a C# DataTable from a SQL Server Table?

Mary-Kate Olsen
Release: 2024-12-29 19:03:15
Original
708 people have browsed it

How to Populate a C# DataTable from a SQL Server Table?

Populating a C# DataTable from a SQL Table

Unlike the common scenario of inserting C# DataTable data into a SQL table, this article explores the technique of extracting data from a SQL table into a C# DataTable.

To accomplish this data transfer, you can utilize the following C# code as a starting point:

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataTableFromSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connection string to your database
            string connectionString = "your connection string here";

            // SQL query to retrieve data
            string query = "select * from table";

            // Create new SQL connection
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                // Create command using query and connection
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    // Open the connection
                    connection.Open();

                    // Create data adapter
                    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                    {
                        // Create new DataTable
                        DataTable dataTable = new DataTable();

                        // Fill DataTable with data from SQL table
                        adapter.Fill(dataTable);

                        // Iterate through rows and print column values
                        foreach (DataRow row in dataTable.Rows)
                        {
                            foreach (DataColumn column in dataTable.Columns)
                            {
                                Console.WriteLine($"{column.ColumnName}: {row[column]}");
                            }
                        }
                    }
                }
            }
        }
    }
}
Copy after login

In this code:

  • Establish a connection to your SQL database using SqlConnection and a connection string.
  • Create a SqlCommand with the specified SQL query and connection.
  • Use a SqlDataAdapter to create a bridge between the SqlCommand and the DataTable.
  • Open the SQL connection.
  • Call the Fill method of the SqlDataAdapter to populate the DataTable with data from the SQL table.
  • Finally, iterate through the DataTable rows and columns to access and display the retrieved data.

The above is the detailed content of How to Populate a C# DataTable from a SQL Server Table?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template