今天小編給大家分享一下如何完成一個(gè)小型工程報(bào)價(jià)數(shù)據(jù)庫(kù)系統(tǒng)的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元芝罘做網(wǎng)站,已為上家服務(wù),為芝罘各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
功能很簡(jiǎn)單,完成基本的增刪改查
搭建項(xiàng)目三層結(jié)構(gòu)
界面的設(shè)計(jì)以及美化
分層代碼的設(shè)計(jì)與實(shí)現(xiàn)
1.模型層:由于表設(shè)計(jì)時(shí)存在主外鍵關(guān)系,故把表映射成對(duì)象時(shí)添加一個(gè)外鍵對(duì)象來(lái)保存外鍵那張表的相關(guān)數(shù)據(jù)。
// 外鍵實(shí)體 private Product _product; private Project _project; public Project _Project { get { return _project; } set { _project = value; } } public Product _Product { get { return _product; } set { _product = value; } }
2.數(shù)據(jù)訪問(wèn)層:把數(shù)據(jù)封裝成Model對(duì)象的各層之間傳遞。
我個(gè)人不喜歡三層之間傳遞DataSet數(shù)據(jù)集,我的理解是盡量不在BLL層出現(xiàn)DataSet,所以我在DAL層把所有獲取的數(shù)據(jù)封裝成IEnumerable集合,然后返回。
protected IEnumerableToModelsByFK(SqlDataReader reader) { var list = new List (); while (reader.Read()) { list.Add(ToModelByFK(reader)); } return list; }
public ProjectItem ToModelByFK(SqlDataReader reader) { ProjectItem projectItem = new ProjectItem(); projectItem._Product = new Product(); projectItem._Project = new Project(); projectItem.ID = (int)ToModelValue(reader, "ID"); projectItem._Product.ID = (int)ToModelValue(reader, "ProductID"); projectItem._Project.ID = (int)ToModelValue(reader,"projectID"); projectItem._Product.Manufacturer = ToModelValue(reader, "Manufacturer").ToString(); projectItem._Product.Parameters = ToModelValue(reader, "Parameters").ToString(); projectItem._Product.Price = decimal.Parse( ToModelValue(reader, "Price").ToString()); projectItem._Product.ProductName = ToModelValue(reader, "ProductName").ToString(); projectItem._Product.Specification = ToModelValue(reader, "Specification").ToString(); projectItem._Product.Unit = ToModelValue(reader, "Unit").ToString(); projectItem.Count = (int)ToModelValue(reader, "Count"); projectItem.TotalMoney = (decimal)ToModelValue(reader, "TotalMoney"); projectItem.UnitPrice = (decimal)ToModelValue(reader, "UnitPrice"); return projectItem; }
protected object ToModelValue(SqlDataReader reader,string columnName) { if(reader.IsDBNull(reader.GetOrdinal(columnName))) { return null; } else { return reader[columnName]; } }
使用GetProjectByCondition方法返回一個(gè)封裝成對(duì)象的 IEnumerable集合
public IEnumerableGetProjectByCondition(string projectName, string customerName, string contract, string tel) { StringBuilder sqlWhere = new StringBuilder("select * from Project where 1=1"); List listParameters = new List (); if (!string.IsNullOrWhiteSpace(projectName)) { sqlWhere.AppendLine(" and projectName like @projectName"); listParameters.Add(new SqlParameter("projectName", "%" + projectName + "%")); } if (!string.IsNullOrWhiteSpace(contract)) { sqlWhere.AppendLine(" and Contact like @Contact"); listParameters.Add(new SqlParameter("Contact", "%" + contract + "%")); } if (!string.IsNullOrWhiteSpace(customerName)) { sqlWhere.AppendLine(" and customer like @customer"); listParameters.Add(new SqlParameter("customer", "%" + customerName + "%")); } if (!string.IsNullOrWhiteSpace(tel)) { sqlWhere.AppendLine(" and tel like @tel"); listParameters.Add(new SqlParameter("tel", "%" + tel + "%")); } using (SqlDataReader reader = SqlHelper.ExecuteDataReader(sqlWhere.ToString(), listParameters.ToArray())) { return ToModels(reader); } }
UpdatePassChecked方法接受一個(gè)對(duì)象,通過(guò)對(duì)象的屬性來(lái)獲取數(shù)據(jù)
public int UpdatePassChecked(ProjectItem projectItem) { string sql = "UPDATE Project " + "SET " + " ProjectName = @ProjectName" + ", Customer = @Customer" + ", Contact = @Contact" + ", Tel = @Tel" + ", DeliveryPlace = @DeliveryPlace" + ", DeliveryTime = @DeliveryTime" + ", TransportCosts = @TransportCosts" + ", PaymentTerm = @PaymentTerm" + ", Bak = @Bak" + " WHERE ID = @ID"; SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ID", projectItem._Project.ID) ,new SqlParameter("@ProjectName", ToDBValue(projectItem._Project.ProjectName)) ,new SqlParameter("@Customer", ToDBValue(projectItem._Project.Customer)) ,new SqlParameter("@Contact", ToDBValue(projectItem._Project.Contact)) ,new SqlParameter("@Tel", ToDBValue(projectItem._Project.Tel)) ,new SqlParameter("@DeliveryPlace", ToDBValue(projectItem._Project.DeliveryPlace)) ,new SqlParameter("@DeliveryTime", ToDBValue(projectItem._Project.DeliveryTime)) ,new SqlParameter("@TransportCosts", ToDBValue(projectItem._Project.TransportCosts)) ,new SqlParameter("@PaymentTerm", ToDBValue(projectItem._Project.PaymentTerm)) ,new SqlParameter("@Bak", ToDBValue(projectItem._Project.Bak)) }; return SqlHelper.ExecuteNonQuery(sql, para); }
Add方法傳入一個(gè)已經(jīng)把數(shù)據(jù)封裝好的對(duì)象,然后返回一個(gè)新的對(duì)象(包含新創(chuàng)建生成的ID)
public Product Add(Product product) { string sql ="INSERT INTO Product (ProductName, Specification, Manufacturer, Parameters, Price, Unit) output inserted.ID VALUES (@ProductName, @Specification, @Manufacturer, @Parameters, @Price, @Unit)"; SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ProductName", ToDBValue(product.ProductName)), new SqlParameter("@Specification", ToDBValue(product.Specification)), new SqlParameter("@Manufacturer", ToDBValue(product.Manufacturer)), new SqlParameter("@Parameters", ToDBValue(product.Parameters)), new SqlParameter("@Price", ToDBValue(product.Price)), new SqlParameter("@Unit", ToDBValue(product.Unit)), }; int newId = (int)SqlHelper.ExecuteScalar(sql, para); return GetByID(newId); }
3.業(yè)務(wù)邏輯層:業(yè)務(wù)邏輯層是項(xiàng)目的核心,業(yè)務(wù)邏輯上的代碼通常在這兒實(shí)現(xiàn)(一言難盡)。
簡(jiǎn)單數(shù)據(jù)驗(yàn)證:
public bool UpdateThroughChecked(ProjectItem projectItem) { if (string.IsNullOrEmpty(projectItem._Project.ProjectName)) { throw new Exception("項(xiàng)目名稱不能為空"); } //顧客姓名可以為空,但聯(lián)系人不能為空,所以數(shù)據(jù)庫(kù)里就應(yīng)該設(shè)計(jì)好 if (string.IsNullOrEmpty(projectItem._Project.Contact)) { throw new Exception("聯(lián)系人姓名不能為空"); } if (string.IsNullOrEmpty(projectItem._Project.Tel)) { throw new Exception("聯(lián)系電話不能為空"); } if (string.IsNullOrEmpty(projectItem._Project.DeliveryPlace)) { throw new Exception("交貨地點(diǎn)不能為空"); } if (string.IsNullOrEmpty(projectItem._Project.DeliveryTime)) { throw new Exception("交貨時(shí)間不能為空"); } if (projectItem._Project.TransportCosts<00) { throw new Exception("運(yùn)輸費(fèi)用不能為負(fù)數(shù)"); } return new DAL.ProjectItemService().UpdatePassChecked(projectItem) > 0; }
計(jì)算總金額:
public decimal GetProductTotalMoney(int projectID) { var list = new DAL.ProjectItemService().GetProductTotalMoney(projectID); decimal totalMoney = 0.00M; foreach (var model in list) { totalMoney+=model.TotalMoney.Value; } return totalMoney; }
根據(jù)查詢條件獲取數(shù)據(jù)集合:
////// 根據(jù)查詢條件獲取數(shù)據(jù)集合 /// /// ///public IEnumerable GetProductsByCondition(string condition) { return new DAL.ProductService().GetProductsByCondition(condition); }
根據(jù)ProjectItemID獲取一條記錄(封裝成對(duì)象)
public Model.ProjectItem GetOneProjectItemByID(int ProjectItemID) { return new DAL.ProjectItemService().GetOneProjectItemByID(ProjectItemID); }
增加一條記錄:
////// 新增一條記錄 根據(jù)條件 /// /// ///public Model.Project AddPassCheckd(Model.Project modelProject) { if (string.IsNullOrEmpty(modelProject.ProjectName)) { throw new Exception("項(xiàng)目名稱不能為空"); } if (string.IsNullOrEmpty(modelProject.Customer)) { throw new Exception("顧客名稱不能為空"); } if (string.IsNullOrEmpty(modelProject.Tel)) { throw new Exception("聯(lián)系電話不能為空"); } if (modelProject.TransportCosts <= 0) { throw new Exception("運(yùn)輸費(fèi)用不能為負(fù)數(shù):("); } return new DAL.ProjectService().Add(modelProject); }
刪除一個(gè)項(xiàng)目:
////// 刪除一個(gè)項(xiàng)目 /// /// ///public bool DeleteProject(int projectID) { bool isSucceed= new BLL.ProjectItemManager().DeleteProjectItemsByProjectID(projectID); isSucceed = new DAL.ProjectService().DeleteByID(projectID) > 0 && isSucceed; return isSucceed; }
4.表現(xiàn)層(UI)
簡(jiǎn)單的窗體里嵌套窗體:
public void LoadProjectListForm() { projectListForm = null; if (projectListForm==null) { projectListForm = new ProjectListForm(); } //去掉邊框 projectListForm.FormBorderStyle = FormBorderStyle.None; projectListForm.TopLevel = false;//窗體是否是頂級(jí)窗體 projectListForm.Dock = DockStyle.Fill;//填充 projectListForm.Show();//不寫看不到 panelContainer.Controls.Clear();//清空 panelContainer.Controls.Add(projectListForm); }
綁定數(shù)據(jù):
我原先思路是直接在DataGridView里綁定對(duì)象的屬性,和GridView一樣使用,但一直無(wú)法綁上去,哪兒錯(cuò)了? |
所以只能曲線救國(guó)了: void LoadData() { var list = new BLL.ProjectItemManager().GetAllProducts(projectID); //dataGridViewProjectItems.DataSource = list;//當(dāng)控件被綁定時(shí)無(wú)法向其添加Row dataGridViewProjectItems.Rows.Clear(); foreach (var model in list) { int i = dataGridViewProjectItems.Rows.Add(); dataGridViewProjectItems.Rows[i].Cells["idColumn"].Value = model.ID; dataGridViewProjectItems.Rows[i].Cells["specificationColumn"].Value = model._Product.Specification; dataGridViewProjectItems.Rows[i].Cells["productNameColumn"].Value = model._Product.ProductName; dataGridViewProjectItems.Rows[i].Cells["manufacturerColumn"].Value = model._Product.Manufacturer; dataGridViewProjectItems.Rows[i].Cells["parametersColumn"].Value = model._Product.Parameters; dataGridViewProjectItems.Rows[i].Cells["productIDColumn"].Value = model._Product.ID; dataGridViewProjectItems.Rows[i].Cells["totalMoneyColumn"].Value = model.TotalMoney; dataGridViewProjectItems.Rows[i].Cells["countColumn"].Value = model.Count; dataGridViewProjectItems.Rows[i].Cells["unitPriceColumn"].Value = model.UnitPrice; dataGridViewProjectItems.Rows[i].Cells["projectIDColumn"].Value = model._Project.ID; } |
以上就是“如何完成一個(gè)小型工程報(bào)價(jià)數(shù)據(jù)庫(kù)系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。