使用 SQL Server 2008 自动生成 VB.NET 表单

2021-09-16 00:00:00 vb.net sql-server winforms

我想使用来自 SQL Server 数据库的表(一个表单/数据库表)自动生成 VB.NET 表单.为它编写自定义代码也许是可能的,但如果已经有一些功能可以很好地完成这项工作(数据库有 40 多个表,手动执行此操作是一项乏味的任务).

I want to automatically generate VB.NET forms using tables from a SQL Server database (one form / database table). It is perhaps possible with writing custom custom code for it, but if there is already some feature that does the job that would be great (database has 40+ tables, manually doing this is a tedious task).

非常感谢任何答案、帮助、链接、提示.

Any answers, help, links, tips is greatly appreciated.

问候,阿尤布

推荐答案

只需一分钟即可修复,所有功能都已在 Visual Studio 中存在.

It takes just a minute to fix, all functionality allready exists in Visual Studio.

启动 Visual Studio,点击添加新数据源... 启动数据源配置向导:

Fire up Visual Studio, click on Add new datasource... to start the Data Source Configuration Wizard:

选择数据库并按照向导操作:

Select Database and follow the wizard:

连接到数据库后,选择您感兴趣的表,然后按芬兰语按钮:

When connected to the database, select the tables you are interrested in and press the Finnish button:

现在,这将在您的解决方案中创建一个强命名数据集,如果您双击 xsd 文件,您将看到您在架构编辑器中选择的表,但暂时保留:

Now, this will create a strongly named dataset in your solution, if you double click the xsd file you'll see the tables you selected in the schema editor, but leave that for now:

现在,从数据菜单中选择显示数据源",您将看到您在向导中选择的所有表.在每个字段的左侧有一个图标,指示该字段将在结果表单上表示哪种类型的控件:

Now, select "Show Data Sources" from the data-menu and you will see all tables you selected in the wizard. To the left of each field its an icon that tells what type of control that field will be represented by on the resulting form:

现在您可以决定如何在表单上显示数据,作为数据网格视图或详细模式,只需使用表名上的下拉菜单(仅在表单设计模式下).

Now you can deside how the data will be presented on the form, as a datagridview or in detail mode, just use the dropdown on the table name (only when in form-design-mode).

如果您在表格上选择了详细信息模式,那么您可以更改字段将被表示的控件(必须是表单设计模式,而不是代码模式):

If you have selected details-mode on the table, then you can change what control the field will be represented by (must be in form-design-mode, not code-mode):

然后只需将表格从数据源视图拖到一个空表单中,它就会神奇地创建控件来编辑/添加/删除和移动数据.

Then just drag the table from the data source view to an empty form and it will magically create controls to edit/add/delete and move around the data.

这是选择 DataGridView-mode 时的结果:

This is the result if DataGridView-mode was selected:

如果在表格中选择了详细信息:

And if Details was selected on the table:

在它后面的代码中,还神奇地添加了一些代码,以便在加载表单时将数据加载到适配器以及一些保存/验证代码:

In code behind it also magically add some code to load the data to the adapter when the form loads and some save/validating code:

Private Sub AccountBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AccountBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.AccountBindingSource.EndEdit()
    Me.AccountTableAdapter.Update(Me.MyDBDataSet.Account)

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'MyDBDataSet.Account' table. You can move, or remove it, as needed.
    Me.AccountTableAdapter.Fill(Me.MyDBDataSet.Account)

End Sub

相关文章