Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

c) Connect with C#

Code Block
themeDJangoRDark
languagec#
linenumberstrue
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;

namespace SecureSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = "Server=sqlseclab-lsnr1.its.yale.edu;" + 
                                              "Initial Catalog=master;" +
                                              "Integrated Security=true;" +
                                              "Encrypt=True;TrustServerCertificate=False";
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "select GetDate()";
                    command.CommandType = CommandType.Text;

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine("{0}", reader[0]);
                        }
                    }
                }

                Console.ReadLine();
            }
        }
    }
}

...