can someone help me finish my C# code here is the whole code using System; using
ID: 3834028 • Letter: C
Question
can someone help me finish my C# code
here is the whole code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;
namespace CIS3309MakupLabExam
{
class PartsDB
{
// Get current date and time as string for insert into MS Access
public static string GetDateTimeString()
{
DateTime dt = DateTime.Now;
String s = "#" + dt.Year + "-" + dt.Month + "-" + dt.Day +
" " + dt.Hour + ":" + dt.Minute + ":" + dt.Second + "#";
return s;
}
// Get the connection string from an external file
public static string GetConnectionString()
{
return File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
}
// Get a connection to DB
public static OleDbConnection GetConnection()
{
// Declare a connection and initialze to null
OleDbConnection conn = null;
// Declare string and call method to get connection string from file
string connStr = File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
conn = new OleDbConnection(connStr);
// Instantiate connection
// Return connection
return conn;
}
// Get a list of all Parts from DB for a Make of vehicle
public static PartsList GetParts(string make)
{
// Declare a PartsList and initialize to null pointer
PartsList pl = null;
// Declare a connection and initialize to null
// Read all Forms from DB in try-catch block
// Try
// Instantiate PartsList
// Declare and get connection
// Open connection
// Declare and copy sql statement to sql string
// Should only select Parts where DB field make equals
// parameter make (passed from frmMain)
// Declare and instantiate the command
// Execute the command reader
// Loop to get all records and copy to PartsList
// Instantiate a Part with constructor, passing data from reader
// Add Product to PartsList
// Catch
// Show error message
// Set List of Product to null pointer
// Finally
// If connection not null close it
// Return PartsList
return pl;
}
// Execute a non-query on DB
public static int ExeNonQuery(string sqlString)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Get connection
OleDbConnection connection = GetConnection();
// Declare and instantiate a command
OleDbCommand command = new OleDbCommand(sqlString, connection);
// Open connection and execute in try-catch
// Try
try
{
connection.Open();
records = command.ExecuteNonQuery();
}
// Open connection
// Execute operation
// Catch
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
records = -1;
}
// Show error message
// Set number of records affected to -1
// Finally
finally
{
connection.Close();
}
// If connection not null close it
// Return number of records affected
return records;
}
// Update Part Quantity
public static int UpdateQuantity(int qtyChange, Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
OleDbConnection connection = GetConnection();
// Declare and set sql statement string to update Quantity in DB
string sqlString = "Update MAX(fldID) FROM tblOrders";
// for Part p using ID. New Quantity reflect the change in Quantity
// in qtyChange
// Execute update - set records to count affected
// Return number of records affected
return records;
}
// Delete a Part
public static int DeletePart(Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Declare and set sql statement string to delete Part p from DB by ID
string sqlString = "DELETE fldMake FROM tblParts WHERE FLDID=1";
// Execute delete - set records to count affected
// Return number of records affected
return records;
}
// Insert a transaction for audit trail
public static int InsertTransaction(string userName, int qtyChange, Part p, string type)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Declare and set sql statement string to insert data into the
// tblTransaction table - use GetDateTimeString() for the Date
// Execute insert - set records to count affected
// Return number of records affected
return records;
}
} // End class
} // End namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CIS3309MakupLabExam
{
// Inherits list of Part
class PartsList : List<Part>
{
// Just inherit List stuff
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CIS3309MakupLabExam
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CIS3309MakupLabExam
{
class Part
{
// Part data
public int ID { get; set; }
public string Make { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
// Constructor that accepts data as parameters
public Part(int ID, string Make, string Name, double Price, int Quantity)
{
this.ID = ID;
this.Make = Make;
this.Name = Name;
this.Price = Price;
this.Quantity = Quantity;
}
// Constructor that accepts Part as paremeter
public Part(Part p)
{
this.ID = p.ID;
this.Make = p.Make;
this.Name = p.Name;
this.Price = p.Price;
this.Quantity = p.Quantity;
}
}
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.OleDb;
using System.Windows.Forms;
namespace CheggWf
{
class PartsDB
{
// Get current date and time as string for insert into MS Access
public static string GetDateTimeString()
{
DateTime dt = DateTime.Now;
String s = "#" + dt.Year + "-" + dt.Month + "-" + dt.Day +
" " + dt.Hour + ":" + dt.Minute + ":" + dt.Second + "#";
return s;
}
// Get the connection string from an external file
public static string GetConnectionString()
{
return File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
}
// Get a connection to DB
public static OleDbConnection OleDbConnectionGetConnection()
{
// Declare a connection and initialze to null
OleDbConnection conn = null;
// Declare string and call method to get connection string from file
string connStr = File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
conn = new OleDbConnection(connStr);
// Instantiate connection
// Return connection
return conn;
}
// Get a list of all Parts from DB for a Make of vehicle
public static List<Part> GetParts(string make)
{
List<Part> pl = null;
OleDbConnection con = null;
try
{
pl = new List<Part>();
con = OleDbConnectionGetConnection();
con.Open();
string sqlQuery = "select * from tblParts where fldMake='" + make + "'";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = sqlQuery;
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows)
{
Part part = new Part(Convert.ToInt32(dr["fldID"]),
Convert.ToString(dr["fldMake"]),
Convert.ToString(dr["fldName"]),
Convert.ToDouble(dr["fldPrice"]),
Convert.ToInt32(dr["fldQuantity"])
);
pl.Add(part);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
pl = null;
}
finally
{
con.Close();
con.Dispose();
}
return pl;
}
// Execute a non-query on DB
public static int ExeNonQuery(string sqlString)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Get connection
OleDbConnection connection = OleDbConnectionGetConnection();
// Declare and instantiate a command
OleDbCommand command = new OleDbCommand(sqlString, connection);
// Open connection and execute in try-catch
// Try
try
{
connection.Open();
records = command.ExecuteNonQuery();
}
// Open connection
// Execute operation
// Catch
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
records = -1;
}
// Show error message
// Set number of records affected to -1
// Finally
finally
{
connection.Close();
}
// If connection not null close it
// Return number of records affected
return records;
}
// Update Part Quantity
public static int UpdateQuantity(int qtyChange, Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
OleDbConnection connection = OleDbConnectionGetConnection();
string sqlString = "Update tblParts set fldPrice=" + qtyChange + " where fldID=" + p.ID + "";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
cmd.CommandText = sqlString;
records = cmd.ExecuteNonQuery();
return records;
}
// Delete a Part
public static int DeletePart(Part p)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
OleDbConnection connection = OleDbConnectionGetConnection();
// Declare and set sql statement string to delete Part p from DB by ID
string sqlString = "DELETE * FROM tblParts WHERE FLDID=" + p.ID + "";
// Execute delete - set records to count affected
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
cmd.CommandText = sqlString;
records = cmd.ExecuteNonQuery();
// Return number of records affected
return records;
}
// Insert a transaction for audit trail
public static int InsertTransaction(string userName, int qtyChange, Part p, string type)
{
// Declare an int to contain a count of records affected by operation
int records = 0;
// Declare and set sql statement string to insert data into the
// tblTransaction table - use GetDateTimeString() for the Date
OleDbConnection connection = OleDbConnectionGetConnection();
// Execute insert - set records to count affected
string sqlString = "insert into tblParts(fldUserName,fldPartsID,fldOldQuantity,fldNewQuantity,fldDate,fldType) values('" + userName + "'," + p.ID + "," + p.Quantity + "," + qtyChange + ",'" + GetDateTimeString() + "','" + type + "')";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
cmd.CommandText = sqlString;
records = cmd.ExecuteNonQuery();
// Return number of records affected
return records;
}
} // End class
} // End namespace
Note: You made so many thinks complexity. Anyhow I made everything as your requested code. You mention list inherits from in separate class but i inserted it in same code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.