从 SQL 存储过程加载数据表
我正在尝试调用 vb.net WCF 程序的存储过程。由于某种原因,它没有在 vb 端返回任何行。它应该是一个简单的 datatable.load(cmd.executereader)。当我在 SQL 中执行存储过程时,我至少得到两行。存储过程没有参数。
这是我的 VB 代码:
Public Function GetClaimsLetterTypes() As List(Of LetterTypes) Implements ILetterWriter.GetClaimsLetterTypes
Dim SQLcon As New SqlClient.SqlConnection
Dim SQLcmd As New SqlClient.SqlCommand
Dim dtTypes As DataTable
Dim rw As DataRow
'Initialize
GetClaimsLetterTypes = New List(Of LetterTypes)
'Connect to the database
SQLcon.ConnectionString = "Data Source=VMSQL08-SRV1;Initial Catalog=Mine;User ID=stupido;Password=opensesame;"
SQLcon.Open()
'Grab the stored procedure, which returns the letter types
SQLcmd.CommandText = "sp_GetTypes"
SQLcmd.CommandType = CommandType.StoredProcedure
SQLcmd.Connection = SQLcon
'Execute the stored procedure, fill the datatable from a data adapter
dtTypes = New DataTable
dtTypes.Load(SQLcmd.ExecuteReader)
'Load the list to be returned
For Each rw In dtTypes.Rows
Dim ltrTypes As New LetterTypes
ltrTypes.ID = rw(0)
ltrTypes.TypeName = rw(1)
'Add the variable to the object list
GetClaimsLetterTypes.Add(ltrTypes)
Next
'Shut it down
SQLcmd.Dispose()
SQLcon.Close()
SQLcon.Dispose()
End Function
我做错了什么?无论出于何种原因,它都不会将行发送回我的 WCF。我有另一个功能几乎相同,但工作得很好。
我错过了什么吗?
谢谢, 贾森
I'm trying to call a stored procedure for a vb.net WCF program. For some reason, it's not returning any rows on the vb side. It should be a simple datatable.load(cmd.executereader). When I execute the stored proc in SQL, I get at least two rows. There are NO parameters for the stored proc.
Here's my VB code:
Public Function GetClaimsLetterTypes() As List(Of LetterTypes) Implements ILetterWriter.GetClaimsLetterTypes
Dim SQLcon As New SqlClient.SqlConnection
Dim SQLcmd As New SqlClient.SqlCommand
Dim dtTypes As DataTable
Dim rw As DataRow
'Initialize
GetClaimsLetterTypes = New List(Of LetterTypes)
'Connect to the database
SQLcon.ConnectionString = "Data Source=VMSQL08-SRV1;Initial Catalog=Mine;User ID=stupido;Password=opensesame;"
SQLcon.Open()
'Grab the stored procedure, which returns the letter types
SQLcmd.CommandText = "sp_GetTypes"
SQLcmd.CommandType = CommandType.StoredProcedure
SQLcmd.Connection = SQLcon
'Execute the stored procedure, fill the datatable from a data adapter
dtTypes = New DataTable
dtTypes.Load(SQLcmd.ExecuteReader)
'Load the list to be returned
For Each rw In dtTypes.Rows
Dim ltrTypes As New LetterTypes
ltrTypes.ID = rw(0)
ltrTypes.TypeName = rw(1)
'Add the variable to the object list
GetClaimsLetterTypes.Add(ltrTypes)
Next
'Shut it down
SQLcmd.Dispose()
SQLcon.Close()
SQLcon.Dispose()
End Function
What am I doing wrong? For whatever reason, it won't send the rows back to my WCF. I have another function that works almost identically, but works just fine.
Am I missing something?
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个代码,它几乎可以替换从命令声明到 foreach 循环结束的所有内容。具有在执行 Read() 时使用 SqlDataReader 的内容的额外好处
Try this code, it can pretty much replace everything from your command declaration to the end of your for each loop. Has the added benefit of using the contents of the SqlDataReader while executing Read()