コード例 #1
0
        public static void SelectByID(int ID)
        {
            SqlConnection con = new SqlConnection(conString);

            con.Open();
            Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine("\nID\t\tFirstname\t\tLastname\t\tMiddlename\t\tBirthday");
            string     commandText = $"Select * from PersonTable where id = '{ID}'";
            SqlCommand command     = new SqlCommand(commandText, con);

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                String tempId     = reader.GetValue(0).ToString();
                String tempFirst  = reader.GetValue(1).ToString();
                String tempSecond = reader.GetValue(2).ToString();
                string tempMiddle = reader.GetValue(3).ToString();
                String tempDate   = reader.GetValue(4).ToString().Trim().Substring(0, 10);

                ConsoleShow.ShowTable(tempId, tempFirst, tempSecond, tempMiddle, tempDate);
            }
            reader.Close();
            Console.ForegroundColor = ConsoleColor.White;
        }
コード例 #2
0
        public static void SelectAll()
        {
            SqlConnection con = new SqlConnection(conString);

            con.Open();

            if (con.State == ConnectionState.Open)
            {
                System.Console.WriteLine("Connected");
            }
            else
            {
                System.Console.WriteLine("Error");
            }
            string     commandText = "Select * from PersonTable";
            SqlCommand command     = new SqlCommand(commandText, con);

            Console.ForegroundColor = ConsoleColor.Green;


            System.Console.WriteLine("\nID\t\tFirstname\t\tLastname\t\tMiddlename\t\tBirthday");

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                String tempId     = reader.GetValue(0).ToString();
                String tempFirst  = reader.GetValue(1).ToString();
                String tempSecond = reader.GetValue(2).ToString();
                string tempMiddle = reader.GetValue(3).ToString();
                String tempDate   = reader.GetValue(4).ToString().Trim().Substring(0, 10);

                ConsoleShow.ShowTable(tempId, tempFirst, tempSecond, tempMiddle, tempDate);
            }
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.Write("\nPlease press any key to continue...");
            Console.ReadKey();
            reader.Close();
        }