Question

For this assignment - you will be writing a program (you may choose whatever programming language...

For this assignment - you will be writing a program (you may choose whatever programming language you want) to read in two of your database tables from Lab 5, and then print out the contents of each of the two tables separately. These MUST be database tables that have already been created and stored as input for your program.

You may use either SQL code to select All of your tables and do a screen capture. Turn in a copy of your SQL code, and a copy of the screen captured tables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi there,

It is so simple. Let me first create 2 tables called Employees and Students.

Below are the C# code files to read data from both the SQL tables.

EmployeeData:

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

namespace EmployeeDetails
{
   class Employee
   {
       static void Main()
       {
           Employee rd = new Employee();
           rd.Read();
       }

       public void Read()
       {
          
           SqlDataReader rdr = null;

           // create a connection object
           SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

           // create a command object
           SqlCommand cmd = new SqlCommand(
               "select * from Employees", conn);

           try
           {
               // open the connection
               conn.Open();

              
               rdr = cmd.ExecuteReader();

               // print a set of column headers
               Console.WriteLine(
"Employee FName Employee LName Hire Date");
               Console.WriteLine(
"------------ ------------ ------------");

              
               while (rdr.Read())
               {
                   // get the results of each column
                   string fname = (string)rdr["first_name"];
                   string lname = (string)rdr["last_name"];
                   string date = (string)rdr["hire_date"];

                   // print out the results
                   Console.Write("{0,-25}", fname);
                   Console.Write("{0,-20}", lname);
                   Console.Write("{0,-25}", date);
                   Console.WriteLine();
               }
           }
           finally
           {
              
               if (rdr != null)
               {
                   rdr.Close();
               }

               // close the connection
               if (conn != null)
               {
                   conn.Close();
               }
           }  
       }
   }
}

StudentsData:

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

namespace StudentDetails
{
   class Student
   {
       static void Main()
       {
           Student rd = new Student();
           rd.Read();
       }

       public void Read()
       {
          
           SqlDataReader rdr = null;

          
           SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

          
           SqlCommand cmd = new SqlCommand(
               "select * from Students", conn);

           try
           {
               // open the connection
               conn.Open();

              
               rdr = cmd.ExecuteReader();

               // print a set of column headers
               Console.WriteLine(
"Student Name Gender Student Age");
               Console.WriteLine(
"------------ ------------ ------------");

              
               while (rdr.Read())
               {
                   // get the results of each column
                   string contact = (string)rdr["StudentName"];
                   string gender = (string)rdr["StudentGender"];
                   string age = (string)rdr["StudentAge"];

                   // print out the results
                   Console.Write("{0,-25}", contact);
                   Console.Write("{0,-20}", gender);
                   Console.Write("{0,-25}", age);
                   Console.WriteLine();
               }
           }
           finally
           {
              
               if (rdr != null)
               {
                   rdr.Close();
               }

               // close the connection
               if (conn != null)
               {
                   conn.Close();
               }
           }  
       }
   }
}

Please feel free to ask if you have any questions. Happy Learning!

Thanks.

Add a comment
Know the answer?
Add Answer to:
For this assignment - you will be writing a program (you may choose whatever programming language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do...

    PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do one of five things at a time: 1. Add students to database. • There should be no duplicate students. If student already exists, do not add the data again; print a message informing student already exists in database. • Enter name, age, and address. 2. Search for students in the database by name. • User enters student name to search • Show student name,...

  • Submissions are accepted in program codes in C PROGRAMMING LANGUAGES COURSE ASSIGNMENT Design your own general purpose programming language which will be completely in english Your language design...

    Submissions are accepted in program codes in C PROGRAMMING LANGUAGES COURSE ASSIGNMENT Design your own general purpose programming language which will be completely in english Your language design must include the following A general skeleton of a program State diagrams for the following (35 pts) . Identifier Unsigned integer Unsigned constant Constant Variable Factor and term rules similar to the ones given in your textbook Simple expression Compound expression . Parameter list Simple type 1D Array type Statement Block Loop...

  • Use C++ language. Write a program that gives and takes advice on program writing. The program...

    Use C++ language. Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the...

  • Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows...

    Pascal programming language Implement a symbol balance checker function for the Pascal programming language. Pascal allows for the following pairs: {}, (), [], begin end . All programs will begin with the word "begin" and end with the word "end". Your function should receive an ifstream object which is already open and will return true, all of the symbols match, or false, they do not. You do not have to worry about comments in the program but you do have...

  • Use Java language to write this program Programming Exercise 3.20 required you to design a PID...

    Use Java language to write this program Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing a program that created a number of threads that requested and released process identifiers. Now modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe from race conditions. Use...

  • Help writing MatLab code Lab Assignment 5-Employees Average Hours CSCI 251 Problem Statement Suppose the weekly...

    Help writing MatLab code Lab Assignment 5-Employees Average Hours CSCI 251 Problem Statement Suppose the weekly hours for all employees are stored in a text file where each line contains the employee name followed by the hours worked for each day of the week. The data is stored as follows (data is continuous in the file but represented in columns below): Kelly Brian Katie Michae Emily Jim John Jane Joe Doe Smith Hart Jones Hu Wright Young Green Hurley Write...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • Using C Programming Assignment: Implementing a Reliable Transport Protocol Overview In this laboratory programming assignment, you...

    Using C Programming Assignment: Implementing a Reliable Transport Protocol Overview In this laboratory programming assignment, you will be writing the sending and receiving transport-level code for implementing a simple reliable data transfer protocol. There are two versions of this lab, the Alternating-Bit-Protocol version and the Go- Back-N version. This lab should be fun since your implementation will differ very little from what would be required in a real-world situation. Since you probably don't have standalone machines (with an OS that...

  • In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to...

    In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that contains at least two ‘A’s. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which asks the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT