public static void Main()
        {
            Boolean isAdmin;

            string UserChoice   = "";
            string returnOutput = "";

            IMaterialDatabaseActivity projec       = new MaterialDatabaseActivitySQL();
            MaterialModel             materialInfo = new MaterialModel();
            materialsView             projView     = new materialsView();

            List <MaterialModel> lstMaterialsModel;

            isAdmin = projView.initialse();

            UserChoice = projView.userOptions(isAdmin);

            if (UserChoice == "Search")
            {
                materialInfo      = projView.userSearchInstrucitons(materialInfo);
                lstMaterialsModel = projec.Search(materialInfo);
                projView.displayMaterialList(lstMaterialsModel);
            }
            else if (UserChoice == "Add")
            {
                materialInfo = projView.displayAddMaterailInstrucitons(materialInfo);
                returnOutput = projec.Add(materialInfo);

                if (returnOutput == "")
                {
                    Console.WriteLine("New Material Added Sucessfully");
                }
                else
                {
                    Console.WriteLine(returnOutput);
                }
            }
            else if (UserChoice == "Update")
            {
                projView.displayUpdateMaterailInstrucitons(materialInfo);
                returnOutput = projec.Update(materialInfo);

                if (returnOutput == "")
                {
                    Console.WriteLine("Material updated Sucessfully");
                }
                else
                {
                    Console.WriteLine(returnOutput);
                }
            }
            else if (UserChoice == "Delete")
            {
                materialInfo.IdentificationNumber = projView.displayDeleteMaterailInstrucitons();
                returnOutput = projec.Delete(materialInfo);

                if (returnOutput == "")
                {
                    Console.WriteLine("Material deleted Sucessfully");
                }
                else
                {
                    Console.WriteLine(returnOutput);
                }
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
示例#2
0
        public List <MaterialModel> Search(MaterialModel MaterialInfo)
        {
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(sqlConnectionText))
                {
                    SqlDataAdapter da        = new SqlDataAdapter();
                    MaterialModel  materials = null;

                    string sqlCmdText = "SELECT * FROM dbo.MaterialInfo"
                                        + " WHERE ProjectTitle = @ProjectTitle OR IdentificationNumber = @IdentificationNumber OR AddedDate = @AddedDate OR MaterialType = @MaterialType";

                    using (SqlCommand cmd = new SqlCommand(sqlCmdText))
                    {
                        cmd.Connection = sqlCon;
                        sqlCon.Open();
                        DataSet ds = new DataSet();

                        if (MaterialInfo.projectTitle != null)
                        {
                            cmd.Parameters.AddWithValue("@ProjectTitle", MaterialInfo.projectTitle);
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@ProjectTitle", DBNull.Value);
                        }

                        if (MaterialInfo.IdentificationNumber != 0)
                        {
                            cmd.Parameters.AddWithValue("@IdentificationNumber", MaterialInfo.IdentificationNumber);
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@IdentificationNumber", DBNull.Value);
                        }

                        if (MaterialInfo.addedDate != null)
                        {
                            cmd.Parameters.AddWithValue("@AddedDate", MaterialInfo.addedDate);
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@AddedDate", DBNull.Value);
                        }

                        if (MaterialInfo.materialType != null)
                        {
                            cmd.Parameters.AddWithValue("@MaterialType", MaterialInfo.materialType);
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@MaterialType", DBNull.Value);
                        }

                        da = new SqlDataAdapter(cmd);
                        da.Fill(ds);

                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            var values = row.ItemArray;

                            materials = new MaterialModel()
                            {
                                IdentificationNumber = Convert.ToInt32(values[0]),
                                title        = values[1].ToString(),
                                projectTitle = values[2].ToString(),
                                materialType = values[3].ToString(),
                                format       = values[4].ToString(),
                                audioFormat  = values[5].ToString(),
                                runTime      = (TimeSpan)(values[6]),
                                releaseDate  = Convert.ToDateTime(values[7]),
                                launguage    = values[8].ToString(),
                                retailPrice  = (float)Convert.ToDouble(values[9]),
                                subTitles    = values[10].ToString(),
                                frameAspect  = values[11].ToString(),
                                packaging    = values[12].ToString(),
                                addedDate    = values[13].ToString(),
                                addedBy      = values[14].ToString()
                            };
                            lstMaterialsModel.Add(materials);
                        }
                        sqlCon.Close();
                    }
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
            }
            return(lstMaterialsModel);
        }