帮助此VB.NET砖墙 [英] Help with this VB.NET brickwall
问题描述
大家好
我正在使用vb.net(vs2010)开发n层应用程序,并且能够通过数据集访问我的数据.
一切正常,直到我必须向数据库提交一些数据为止.
这是我在DataManager中的功能:
Hi All
I am developing an n-tier application using vb.net (vs2010) and have been able to access my data through a dataset.
Everything works very well until I have to submit some data to the database.
This is my function in the DataManager:
Public Function SaveReceipt(ByRef NewReceipt As ZHRISBLL.ExamsRegisterDataSet) As Boolean
Dim saved = False
If NewReceipt.HasChanges AndAlso Not NewReceipt.HasErrors Then
Dim Manager As New ZHRISDAL.ExamsRegisterDataSetTableAdapters.TableAdapterManager
Manager.InsertReceiptTableAdapter = New ZHRISDAL.ExamsRegisterDataSetTableAdapters.InsertReceiptTableAdapter
Manager.BackupDataSetBeforeUpdate = True
saved = (Manager.UpdateAll(NewReceipt) > 0)
End If
Return saved
End Function
上面的代码应该用于将数据保存到数据库表中.其他功能正在成功保存
我想保存到具有IDENTITY列的表中,该列为该条目生成主键,然后我的表适配器必须提取所生成的PK并将其发送回客户端. (我正在使用SCOPE_IDENTITY SQL函数,并且能够查看数据源设计中Dataset.TableAdapter Preview Data工具中返回的值.)但是,我看不到新记录插入到数据库中,但是如果我将记录插入到数据库中,手动创建数据库表时,自动生成的PK高于VS2010数据集设计器TableAdapter预览中返回的PK.
要尝试从客户端插入新创建的数据,我需要以下代码来处理来自客户端表单的click事件:
The above code is supposed to handle saving data to the database table. Other functions are saving successfully
I want to save to a table with an IDENTITY column that generates the Primary Key for the entry, and then my table adapter has to extract the generated PK and send it back to the client. (I am using the SCOPE_IDENTITY SQL function and have been able to see the value returned in the Dataset.TableAdapter Preview Data facility in Data Source Design) However I do not see the new record inserted into the database, but if I insert a record into the database table manually the autogenerated PK is higher than the one returned in VS2010 Dataset Designer TableAdapter preview.
To try and insert the newly created data from the client side I have the following code handling the click event from the client form:
Private Sub btnSaveAndPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAndPrint.Click
Using proxy As New ZHRISService.ServiceClient
Dim changes = CType(Me.ExamsRegisterDataSet.GetChanges, ZHRISBLL.ExamsRegisterDataSet)
If changes IsNot Nothing Then
If proxy.SaveReceipt(changes) Then
Dim addedrows = From row In Me.ExamsRegisterDataSet.Receipts _
Where row.RowState = DataRowState.Added
For Each row In addedrows.ToArray
row.Delete()
Next
Me.ExamsRegisterDataSet.Merge(changes)
Me.ExamsRegisterDataSet.AcceptChanges()
MsgBox("Receipt Generated")
Else
MsgBox("Receipt not Generated")
End If
End If
End Using
End Sub
我分别在Service和IService中有以下代码
IService:
I have the following code in the Service and IService respectively
IService:
<OperationContract()> _
Function SaveReceipt(ByRef NewReceipt As ExamsRegisterDataSet) As Boolean
服务:
Service:
Public Function SaveReceipt(ByRef NewReceipt As ZHRISBLL.ExamsRegisterDataSet) As Boolean Implements IService.SaveReceipt
Dim Manager As New ZHRISDataUpdateManager
Return (Manager.SaveReceipt(NewReceipt))
End Function
运行代码时,我没有看到任何错误,但显示消息未生成收据".
我是否可以在以下摘要中获得帮助:
1.保存到数据库并返回事务的生成主键的函数.
2.适用于我的客户表单的click事件的正确代码,以便他们能够使用数据库中新生成的PK对其表单进行进一步的操作.
请注意,我真的是vb.net的新手,但是学习速度很快,即使对我的方法和代码提出了批评,也可以提供任何帮助.
谢谢
When I run my code I do not get any errors but the message "Receipt Not Generated".
Could I get help on the following in summary:
1. The Function that saves to the database and returns the transaction''s generated Primary Key.
2. The correct Code for my client form''s click event so that they will be able to use the newly generated PK from the database for further operations on their form.
Take note I am really new to vb.net but a fast learner, any help is appreciated even if it''s criticism on my approach and code.
Thanks
推荐答案
如果我是我,我会在数据库中创建一个存储过程,该过程返回已保存数据的身份(@@ identity).
您将能够分别测试sp,然后只需输入参数(要保存的数据)即可.
这样可以简化代码,提高效率,同时也更易于测试.
If I were you I would create a stored procedure in the database which returns the identity of the saved data (@@identity).
You will be able to test the sp separately and then all you need to do is pass in the parameters (hte data you want to save).
This will simplify your code and be more efficient while also being easier to test.
这篇关于帮助此VB.NET砖墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!