Create a small Family database with one table to include data about members of y
ID: 3808524 • Letter: C
Question
Create a small Family database with one table to include data about members of your family. Include data fields such as first name, last name, type of relationship, hometown, and age. Include one field that uniquely identifies each record, such as a family member number. You can be creative with the family member number or use the auto-generated number from the database. Populate the database with members of your family. Be sure to include data about yourself in the table. Place at least 10 records in your database table, even if it is necessary to make up information. Write a C# program to display all of the data that appears in the database table on a data grid.
Explanation / Answer
DATABASE -
DROP TABLE IF EXISTS `members`;
CREATE TABLE `members` (
`memberId` bigint(20) NOT NULL AUTO_INCREMENT,
`firstname` varchar(255) NOT NULL DEFAULT '',
`lastname` varchar(255) NOT NULL DEFAULT '',
`relationship` varchar(255) NOT NULL DEFAULT '',
`hometown` varchar(255) NOT NULL DEFAULT '',
`age` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`memberId`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
INSERT INTO `members` VALUES (1,'anmol','rai','brother','delhi',23);
INSERT INTO `members` VALUES (2,'sandip','nepal','sister','nepal',23);
INSERT INTO `members` VALUES (3,'rahul','kumar','father','raebareli',52);
INSERT INTO `members` VALUES (4,'akash','tripathi','mother','faizabad',20);
INSERT INTO `members` VALUES (5,'abhishek','pandey','brother','ghaziabad',18);
INSERT INTO `members` VALUES (6,'azad','singh','nephew','saharanpur',22);
INSERT INTO `members` VALUES (7,'shivansh','bhatnagar','mother','moradabad',54);
INSERT INTO `members` VALUES (8,'faiz','khan','niece','meerut',16);
INSERT INTO `members` VALUES (9,'shaksham','gupta','sister','delhi',35);
INSERT INTO `members` VALUES (10,'manoj','tiwary','grand father','etah',87);
PROGRAM -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace check1
{
class Members
{
static void Main(string[] args)
{
using(SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Server=[localhost];Database=[family];Trusted_Connection=true";
conn.open();
SqlCommand command = new SqlCommand("SELECT * FROM members, conn);
using (SqlDataReader reader = command.ExecuteReader())
{
Console.WriteLine("memberId firstname lastname relationship hometown age");
while (reader.Read())
{
Console.WriteLine(String.Format("{0} | {1} | {2} | {3} {4} {5}",
reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]));
}
}
Console.WriteLine("Data displayed! Now press enter to move to the next section!");
Console.ReadLine();
Console.Clear();
}
}
}
}
EXPLANATION -
This is a program that displays the database records on the console.
The very first thing you need to do is create a connection . After that you need to write sql query to get data from the database.
After that we just need to display the data on the screen.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.