SQL C#

Select Version

This C# program demonstrates how to select the SQL server version for Microsoft SQL in C#.

Program.cs

using System;
using System.Data.SqlClient;

namespace SelectVersion {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(SelectVersion());
        }

        public static string SelectVersion() {
            // The using block ensures that the connection is closed when it exits this block.
            using (SqlConnection qConnection = new SqlConnection(
                    @"Server=localhost\SQLEXPRESS01;Trusted_Connection=True;")) {
                SqlCommand qCommand = qConnection.CreateCommand();
                qCommand.CommandText = "SELECT @@version";
                qCommand.Connection.Open();
                return qCommand.ExecuteScalar().ToString();
            }
        }
    }
}
 

Output

Microsoft SQL Server 2017 (RTM-GDR) (KB4494351) - 14.0.2014.14 (X64)
        Apr  5 2019 09:18:51
        Copyright (C) 2017 Microsoft Corporation
        Express Edition (64-bit) on Windows 10 Home 10.0 <X64> (Build 17763: )

Press any key to continue . . .
 
 

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