SQL C#

Reading with an Inner Join

This C# program demonstrates how to read data from an SQL database and perform an inner join over two tables with Microsoft SQL in C#.

Program.cs

using System;
using System.Data.SqlClient;

namespace ReadWithInnerJoin {
    class Program {
        static void Main(string[] args) {
            String sConnectionString = @"Server=LAPTOP-FHJLCP14\SQLEXPRESS;Trusted_Connection=True;Database=XoaX;";
            string sSelectString = "SELECT FeastDays.Title, Apostles.name "+
                "FROM FeastDays "+
                "INNER JOIN Apostles ON FeastDays.ID=Apostles.id;";
            using (SqlConnection qConnection = new SqlConnection(sConnectionString)) {
                SqlCommand qSqlCommand = new SqlCommand(sSelectString, qConnection);
                qConnection.Open();

                SqlDataReader qSqlReader = qSqlCommand.ExecuteReader();
                // Read a single row, if it exists
                while (qSqlReader.Read()) {
                    Console.WriteLine(String.Format("{0}, {1}", qSqlReader[0], qSqlReader[1]));
                }
                qSqlReader.Close();
            }
        }
    }
}
 

Output

The Chains of Saint Peter, Peter
Pope Saint Linus, Andrew
Saint Peter's Chair, Philip
Saint Joseph the Worker, Matthew
Press any key to continue . . .
 

Feast Days Table

Feast Days Table
 

Apostles Table

Apostles Table
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.