免费Windows窗体代码生成器(WinFormsGen Express)教程
这是一个关于如何使用 WinFormsGen 生成代码的教程。
免费 WinForms 代码生成器
WinFormsGen Express是一个免费的 Windows 窗体 (Winforms) 代码生成软件,您可以完全免费下载,没有任何附加条件。WinFormsGen Express 通过读取您的 MS SQL 数据库来生成 WinForms、代码隐藏、中间层代码和数据层代码(其中包含您可以输入自己代码的空白)。它通过一键操作生成 WinForms 应用程序(因此您知道所有生成的代码都有效)。
您将需要什么
我的营销宣传已经讲完了,现在让我们开始教程。
您需要以下内容才能完成本教程:
- Northwind 数据库
- Microsoft SQL Server
- 您将需要管理员用户名/密码或 MS SQL Server 中的足够权限。空白用户名/密码将不起作用。
- WinFormsGen Express
教程
1. 在此处下载 WinFormsGen Express:https://junnark.com/Products/WinFormsGen/DownloadExpress。
2. 安装它。只需按照所有提示操作即可。如果您需要教程,请访问此处:https://junnark.com/Content/PDF/InstallationGuideForWinFormsGen.pdf。
3. 打开 WinFormsGen Express 并输入以下快照中显示的信息。注意:请使用您数据库的用户名和密码,而不是下面显示的。
4. 点击“为所有表生成代码”按钮。WinFormsGen Express 将开始生成代码,代码生成完成后您将看到一条消息。
5. 打开 Visual Studio 2013/2015,然后打开新创建的 Windows 窗体项目。
6. Visual Studio 解决方案资源管理器窗口显示所有生成的 WinForms 和其他对象。
7. 按 F5 运行项目。
8. 点击其中一个链接,例如UnboundProducts。您会注意到“产品”表中的每个字段都显示在此处。外键也显示为组合框控件。还有验证,例如这里的“产品名称”是必填字段。注意:WinFormsGen Express 仅生成未绑定(未绑定到数据库的 Winforms)的 Winforms。
9. 到目前为止您看到的是生成的 WinForms 及其各自的代码隐藏。WinFormsGen Express 还生成了中间层和数据层代码。关闭 Win 窗体,然后返回 Visual Studio。中间层(业务对象)和数据层(数据层)代码位于 Code 文件夹中。WinFormsGen Express 还为它生成的每个 CRUD(创建、检索、更新、删除)操作生成了示例代码。您可以轻松地在 Code 文件夹下的 Example 文件夹中找到所有这些代码。数据库中的每个表都应该有一个类文件。
10. 由于 WinFormsGen Express 不生成将 Windows 窗体绑定到数据库的代码,让我们通过向 UnboundProducts 类 (.cs) 文件添加逻辑来将 WinForm 绑定到数据库。打开 Example 文件夹下的 ProductsExample.cs (.vb) 类文件,然后将 Insert 方法中的代码复制到代码隐藏文件Products.aspx.cs (.vb) BtnAddRecord_Click 事件中。
注意:我们将使用分层方法(三层,n层),因此,WinForm(表示层/前端)将访问 Products.cs (.vb)(中间层),然后Product.cs 将访问ProductsDataLayer.cs(数据层)代码。数据层代码将访问我们的数据库 (MS SQL)。
产品示例类
产品示例插入方法。 复制下面显示的插入方法代码。
产品未绑定用户控件 (UnboundAddEditProducts.cs)。 这是复制插入方法代码之前此 WinForm 的样子。注意:我们在这里使用用户控件,因为相同的用户控件可以被添加或更新 Win 窗体使用。将插入方法代码复制到“在此处放置您的代码”的位置。
从 ProductsExample 类复制插入代码后的产品未绑定 WinForm 代码 (UnboundAddEditProducts.cs)
private void AddOrUpdateProducts() { // first instantiate a new Products Products objProducts = new Products(); // assign values you want inserted objProducts.ProductName = "Chai"; objProducts.SupplierID = 1; objProducts.CategoryID = 1; objProducts.QuantityPerUnit = "1020"; objProducts.UnitPrice = Convert.ToDecimal(18.0000); objProducts.UnitsInStock = 39; objProducts.UnitsOnOrder = 0; objProducts.ReorderLevel = 10; objProducts.Discontinued = false; // finally, insert a new record // the insert method returns the newly created primary key int newlyCreatedPrimaryKey = objProducts.Insert(); }
11. 将常量值更改为各自 WinForm 控件的值。注意:供应商 ID 和类别 ID 组合框 没有值,请确保向这些控件添加项。下面显示的代码只是快速且粗糙的,您需要在转换控件的值之前检查空值或空字符串,但您已经知道这一点。
private void AddOrUpdateProducts() { // first instantiate a new Products Products objProducts = new Products(); objProducts.ProductName = TxtProductName.Text.ToString(); objProducts.SupplierID = Convert.ToInt32(CbxSupplierID.SelectedValue.ToString()); objProducts.CategoryID = Convert.ToInt32(CbxCategoryID.SelectedValue.ToString()); objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text.ToString(); objProducts.UnitPrice = Convert.ToDecimal(MtbUnitPrice.Text.Trim().Replace(",", "").Replace(" ", "")); objProducts.UnitsInStock = Convert.ToInt16(MtbUnitsInStock.Text.Trim().Replace(",", "").Replace(" ", "")); objProducts.UnitsOnOrder = Convert.ToInt16(MtbUnitsOnOrder.Text.Trim().Replace(",", "").Replace(" ", "")); objProducts.ReorderLevel = Convert.ToInt16(MtbReorderLevel.Text.Trim().Replace(",", "").Replace(" ", "")); objProducts.Discontinued = CbxDiscontinued.Checked; // finally, insert a new record // the insert method returns the newly created primary key int newlyCreatedPrimaryKey = objProducts.Insert(); }
12. 以下代码通过中间层对象访问ProductsDataLayerBase.cs(数据层)。
BtnSave_Click 事件 (WinForm/演示层) 调用中间层 Insert() 方法
// finally, insert a new record // the insert method returns the newly created primary key int newlyCreatedPrimaryKey = objProducts.Insert();
ProductBase.cs (中间层/业务对象) 调用数据层 Insert() 方法
public int Insert() { Products objProducts = (Products)this; return ProductsDataLayer.Insert(objProducts); }
public static int Insert(Products objProducts) { // add your code here throw new NotImplementedException(); }
13. 您仍然需要在ProductDataLayerBase.cs 插入方法中添加代码,该代码将您传递的值插入到数据库中。
将UI层传递的中间层值分配给您拥有的任何对象。您的对象可以是Entity Framework,或您希望用于与数据库交互的任何东西。或者您可以直接购买WinFormsGen Professional Plus,这样您甚至不需要担心数据层,它也应该生成这部分以及相应的存储过程(抱歉做广告了)。
public static int Insert(Products objProducts) { // add your code here instantiate your own object // add your code here // assign values to your own object YourCodeToSaveValueToDbaseHere.Whatever = objProducts.ProductName; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.SupplierID; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.CategoryID; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.QuantityPerUnit; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitPrice; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitsInStock; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.UnitsOnOrder; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.ReorderLevel; YourCodeToSaveValueToDbaseHere.Whatever = objProducts.Discontinued; // add your code here to insert to database }
最后的话:
作为程序员,我们为一个项目开发,直到完成或被调到另一个项目。这意味着重复使用相同的数据库,向现有表和/或视图添加更多表或字段,或更新它们。“一键”功能在您下次为同一数据库生成代码时会非常方便,您只需在处理同一数据库时点击“为所有表生成代码”按钮,就这么简单。WinFormsGen Express 完全免费,无需注册,没有弹出窗口要求您购买专业增强版,没有任何营销/垃圾邮件,是的——它完全免费。
一如既往,代码和文章均按“原样”提供,不作任何保证。使用风险自负。
祝你编码愉快!!!
原文:https://www.junnark.com/Blog/Detail/free-windows-form-code-generator-winformsgen-express-tutorial1