![Starting Out With Visual C# (5th Edition)](https://www.bartleby.com/isbn_cover_images/9780135183519/9780135183519_largeCoverImage.gif)
The variables used in the program are given below:
- connectionStringBuilder: an instance of the SqlConnectionStringBuilder class used to create a connection string to the productDB
database . - connectionString: a string that holds the connection string to the database.
- connection: an instance of the SqlConnection class used to connect to the database.
- query: a LINQ query that selects all the products in the database and sorts them by units on hand in ascending order.
- minUnitsOnHand: an integer variable that holds the minimum units on hand specified by the user.
- hasMin: a boolean variable that indicates whether a minimum number of units on hand was specified by the user.
- maxUnitsOnHand: an integer variable that holds the maximum units on hand specified by the user.
- hasMax: a boolean variable that indicates whether a maximum number of units on hand was specified by the user.
The methods used in the program are as follows:
- The SqlConnectionStringBuilder constructor is used to create a connection string to the database.
- The SqlConnection constructor, which creates a new SqlConnection object that can be used to connect to the database.
- The SqlCommand constructor creates a new SqlCommand object that can be used to execute a query against the database.
- The ExecuteReader method of the SqlCommand class is used to execute the query and return a SqlDataReader object that can be used to read the results.
- The Where method of the IQueryable interface is used to filter the data based on a specified condition.
- The TryParse method of the int class is used to convert user input from a string to an integer.
- The Console.WriteLine method, which is used to display information to the user in the console window.
- The Console.ReadLine method, which is used to read user input from the console window.
Program Description:
To write an application that connects to productDB database.
Steps to create an application that connects to a database, sorts, and searches data using LINQ and SQL:
- Create a new C# console application project in Visual Studio.
- Add a reference to the System.Data.SqlClient and System.Linq namespaces.
- Create a connection string to the productDB database using SqlConnectionStringBuilder.
- Create a SqlConnection object and open the connection to the database.
- Create a LINQ query to select all the products in the database and sort them by units on hand in ascending order. You can use the OrderBy method in LINQ to sort the data.
- Execute the query using the SqlCommand object and read the data using the SqlDataReader object.
- Display the data to the user.
- Prompt the user to enter a minimum and/or maximum number of units on hand.
- Modify the LINQ query to filter the products based on the user's input. You can use the Where method in LINQ to filter the data.
- Execute the modified query and display the filtered data to the user.
- Close the SqlDataReader and SqlConnection objects.
Program:
using System; using System.Data.SqlClient; using System.Linq; namespace ProductUnitsOnHandSearch { class Program { static void Main(string[] args) { // Create connection string to the productDB database var connectionStringBuilder = new SqlConnectionStringBuilder { DataSource = "your_server_name", // Replace with your actual database name InitialCatalog = "productDB", IntegratedSecurity = true }; string connectionString = connectionStringBuilder.ToString(); // Create SqlConnection object and open connection using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Create LINQ query to select all products and sort by units on hand in ascending order var query = from p in db.Products orderby p.UnitsOnHand ascending select p; // Execute query and read data using (SqlCommand command = new SqlCommand(query.ToString(), connection)) { using (SqlDataReader reader = command.ExecuteReader()) { // Display all products Console.WriteLine("All Products:"); Console.WriteLine("--------------"); while (reader.Read()) { Console.WriteLine("{0}: {1} units on hand", reader["ProductName"], reader["UnitsOnHand"]); } Console.WriteLine(); } } // Prompt user for minimum and maximum units on hand Console.Write("Enter minimum units on hand (or leave blank for no minimum): "); string minInput = Console.ReadLine(); int minUnitsOnHand; bool hasMin = int.TryParse(minInput, out minUnitsOnHand); Console.Write("Enter maximum units on hand (or leave blank for no maximum): "); string maxInput = Console.ReadLine(); int maxUnitsOnHand; bool hasMax = int.TryParse(maxInput, out maxUnitsOnHand); // Modify query to filter by units on hand based on user input if (hasMin) { query = query.Where(p => p.UnitsOnHand >= minUnitsOnHand); } if (hasMax) { query = query.Where(p => p.UnitsOnHand <= maxUnitsOnHand); } // Execute modified query and display filtered products using (SqlCommand command = new SqlCommand(query.ToString(), connection)) { using (SqlDataReader reader = command.ExecuteReader()) { Console.WriteLine("Filtered Products:"); Console.WriteLine("------------------"); while (reader.Read()) { Console.WriteLine("{0}: {1} units on hand", reader["ProductName"], reader["UnitsOnHand"]); } } } } Console.ReadLine(); } } }
Explanation:
The program is a console application that connects to a database called productDB and displays information about the products in the database. The program uses LINQ and SQL to interact with the database and allows the user to search for products with more units on hand than a specified amount or fewer units on hand than a specified amount.
When the program starts, it creates a connection string to the productDB database and opens a connection to the database using the SqlConnection and SqlCommand classes. The program then executes a LINQ query to select all the products in the database and sorts them by units on hand in ascending order.
The program then prompts the user to enter a minimum unit on hand and a maximum unit on hand. If the user enters a valid integer for the minimum or maximum units on hand, the program filters the results using the Where method of the IQueryable interface. If the user does not enter a value for the minimum or maximum units on hand, the program skips that filter.
Finally, the program loops through the results and displays the product ID, name, and units on hand for each product that matches the user's search criteria. If no products match the criteria, the program displays a message indicating that no results were found. The user can then exit the program or search for products again.
SAMPLE OUTPUT:
Enter minimum units on hand (press enter to skip): 50 Enter maximum units on hand (press enter to skip): 100 Products with units on hand between 50 and 100: ---------------------------------------------- Product ID: 2 Name: Rechargeable Lamp Units on hand: 75 Product ID: 4 Name: Smart watch Units on hand: 80 Do you want to search again? (Y/N) y Enter minimum units on hand (press enter to skip): Enter maximum units on hand (press enter to skip): 200 Products with units on hand between 0 and 200: ---------------------------------------------- Product ID: 1 Name: Wireless Earbuds Units on hand: 120 Product ID: 3 Name:Head phones Units on hand: 150 Do you want to search again? (Y/N) n Press any key to exit...
![Check Mark](/static/check-mark.png)
Want to see the full answer?
Check out a sample textbook solution![Blurred answer](/static/blurred-answer.jpg)
Chapter 14 Solutions
Starting Out With Visual C# (5th Edition)
- Find the error, assume data is a string and all variables have been declared. for ch in data: if ch.isupper: num_upper = num_upper + 1 if ch.islower: num_lower = num_lower + 1 if ch.isdigit: num_digits = num_digits + 1 if ch.isspace: num_space = num_space + 1arrow_forwardFind the Error: date_string = input('Enter a date in the format mm/dd/yyyy: ') date_list = date_string.split('-') month_num = int(date_list[0]) day = date_list[1] year = date_list[2] month_name = month_list[month_num - 1] long_date = month_name + ' ' + day + ', ' + year print(long_date)arrow_forwardFind the Error: full_name = input ('Enter your full name: ') name = split(full_name) for string in name: print(string[0].upper(), sep='', end='') print('.', sep=' ', end='')arrow_forward
- Please show the code for the Tikz figure of the complex plane and the curve C. Also, mark all singularities of the integrand.arrow_forward11. Go to the Webinars worksheet. DeShawn wants to determine the number of webinars the company can hold on Tuesdays and Thursdays to make the highest weekly profit without interfering with consultations, which are also scheduled for Tuesdays and Thursdays and use the same resources. Use Solver to find this information as follows: a. Use Total weekly profit as the objective cell in the Solver model, with the goal of determining the maximum value for that cell. b. Use the number of Tuesday and Thursday sessions for the five programs as the changing variable cells. c. Determine and enter the constraints based on the information provided in Table 3. d. Use Simplex LP as the solving method to find a global optimal solution. e. Save the Solver model below the Maximum weekly profit model label. f. Solve the model, keeping the Solver solution. Table 3: Solver Constraints Constraint Cell or Range Each webinar is scheduled at least once on Tuesday and once on Thursday B4:F5 Each Tuesday and…arrow_forwardGo to the Webinars DeShawn wants to determine the number of webinars the company can hold on Tuesdays and Thursdays to make the highest weekly profit without interfering with consultations, which are also scheduled for Tuesdays and Thursdays and use the same resources. Use Solver to find this information as follows: Use Total weekly profit as the objective cell in the Solver model, with the goal of determining the maximum value for that cell. Use the number of Tuesday and Thursday sessions for the five programs as the changing variable cells. Determine and enter the constraints based on the information provided in Table 3. Use Simplex LP as the solving method to find a global optimal solution. Save the Solver model below the Maximum weekly profit model label. Solve the model, keeping the Solver solution. Table 3: Solver Constraints Constraint Cell or Range Each webinar is scheduled at least once on Tuesday and once on Thursday B4:F5 Each Tuesday and Thursday…arrow_forward
- I want to ask someone who has experiences in writing physics based simulation software. For context I am building a game engine, and want to implement physics simulation. There are a few approaches that I managed to find, but would like to know what are other approaches to doing physics simulation entry points from scenes, would you be able to visually draw me a few approaches (like 3 approaces)? When I say entry point to the actual physics simulation. An example of this is when the user presses the play button in the editor, it starts and initiates the physics system. Applying all of the global physics settings parameters that gets applied to that scene. Here is the use-case, I am looking for. If you have two scenes, and select scene 1. You press the play button. The physics simulation starts. When that physics simulation starts, you are also having to update the physics through some physics dedicated delta time because physics needs to happen faster update frequency. To elaborate,…arrow_forwardI want to ask someone who has experiences in writing physics based simulation software. For context I am building a game engine, and want to implement physics simulation. There are a few approaches that I managed to find, but would like to know what are other approaches to doing physics simulation entry points from scenes, would you be able to visually draw me a few approaches (like 3 approaces)?When I say entry point to the actual physics simulation. An example of this is when the user presses the play button in the editor, it starts and initiates the physics system. Applying all of the global physics settings parameters that gets applied to that scene.Here is the use-case, I am looking for. If you have two scenes, and select scene 1. You press the play button. The physics simulation starts. When that physics simulation starts, you are also having to update the physics through some physics dedicated delta time because physics needs to happen faster update frequency.To elaborate, what…arrow_forwardMale comedians were typically the main/dominant star of television sitcoms made during the FCC licensing freeze. Question 19 options: True False In the episode of The Honeymooners that you watched this week, why did Alice decide to get a job outside of the home? Question 1 options: to earn enough money to buy a mink coat to have something to do while the kids were at school to pay the bills after her husband got laid offarrow_forward
- After the FCC licensing freeze was lifted, sitcoms featuring urban settings and working class characters became far less common. Question 14 options: True Falsearrow_forwardsolve this questions for me .arrow_forwarda) first player is the minimizing player. What move should be chosen?b) What nodes would not need to be examined using the alpha-beta pruning procedure?arrow_forward
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L
- Enhanced Discovering Computers 2017 (Shelly Cashm...Computer ScienceISBN:9781305657458Author:Misty E. Vermaat, Susan L. Sebok, Steven M. Freund, Mark Frydenberg, Jennifer T. CampbellPublisher:Cengage LearningFundamentals of Information SystemsComputer ScienceISBN:9781305082168Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102124/9781337102124_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305657458/9781305657458_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305082168/9781305082168_smallCoverImage.gif)