問題描述
我想使用來自 SQL Server 數(shù)據(jù)庫的表(一個表單/數(shù)據(jù)庫表)自動生成 VB.NET 表單.為它編寫自定義代碼也許是可能的,但如果已經(jīng)有一些功能可以很好地完成這項工作(數(shù)據(jù)庫有 40 多個表,手動執(zhí)行此操作是一項乏味的任務(wù)).
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,點擊添加新數(shù)據(jù)源... 啟動數(shù)據(jù)源配置向?qū)?
Fire up Visual Studio, click on Add new datasource... to start the Data Source Configuration Wizard:
選擇數(shù)據(jù)庫并按照向?qū)Р僮?
Select Database and follow the wizard:
連接到數(shù)據(jù)庫后,選擇您感興趣的表,然后按芬蘭語按鈕:
When connected to the database, select the tables you are interrested in and press the Finnish button:
現(xiàn)在,這將在您的解決方案中創(chuàng)建一個強命名數(shù)據(jù)集,如果您雙擊 xsd 文件,您將看到您在架構(gòu)編輯器中選擇的表,但暫時保留:
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:
現(xiàn)在,從數(shù)據(jù)菜單中選擇顯示數(shù)據(jù)源",您將看到您在向?qū)е羞x擇的所有表.在每個字段的左側(cè)有一個圖標,指示該字段將在結(jié)果表單上表示哪種類型的控件:
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:
現(xiàn)在您可以決定如何在表單上顯示數(shù)據(jù),作為數(shù)據(jù)網(wǎng)格視圖或詳細模式,只需使用表名上的下拉菜單(僅在表單設(shè)計模式下).
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).
如果您在表格上選擇了詳細信息模式,那么您可以更改字段將被表示的控件(必須是表單設(shè)計模式,而不是代碼模式):
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):
然后只需將表格從數(shù)據(jù)源視圖拖到一個空表單中,它就會神奇地創(chuàng)建控件來編輯/添加/刪除和移動數(shù)據(jù).
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 時的結(jié)果:
This is the result if DataGridView-mode was selected:
如果在表格中選擇了詳細信息:
And if Details was selected on the table:
在它后面的代碼中,還神奇地添加了一些代碼,以便在加載表單時將數(shù)據(jù)加載到適配器以及一些保存/驗證代碼:
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
這篇關(guān)于使用 SQL Server 2008 自動生成 VB.NET 表單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!