在SQLDataAccess.dll中抛出了异常:'System.NotImplementedException'
我正在尝试从SQL Server加载一些数据,如下所示:
Imports System.Data.SqlClient
'Imports Microsoft.SqlServer
Public Class Form1
Inherits System.Windows.Forms.Form
'Create ADO.NET objects.
Private myConn As SqlConnection
Private myReader As SqlDataReader
Private results As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=Deportes;" &
"Data Source=localhost;Integrated Security=SSPI;")
'Open the connection.
myConn.Open()
'Create a Command object.
Dim strSQL As String = "SELECT TOP FROM [Deportes].[dbo].[LeaguePower]"
Dim myCmd As New SqlCommand(strSQL, myConn)
myReader = myCmd.ExecuteReader()
'Concatenate the query result into a string.
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab &
myReader.GetString(1) & vbLf
Loop
'Display results.
MsgBox(results)
End
End Sub
当它执行到 ExecuteReader 方法并进入该函数时就失败了。它会调用该命令,然后返回错误:
Exception thrown: 'System.NotImplementedException' in SQLDataAccess.dll
The program '[12808] SQLDataAccess.exe' has exited with code 3221226525 (0xc000041d).
我这么学到的,并且到处都没有看到要处理 ExecuteReader 命令的地方。我原以为这是由Visual Studio处理的。连接打开得很顺利,我可以看到表和数据。
Friend Class SqlCommand
Private strSQL As String
Private myConn As SqlConnection
Public Sub New(strSQL As String, myConn As SqlConnection)
Me.strSQL = strSQL
Me.myConn = myConn
End Sub
Public Property CommandText As String
Public Property CommandType As CommandType
Friend ReadOnly Property ExecuteReader As SqlDataReader
Get
Throw New NotImplementedException()
End Get
End Property
End Class
解决方案
你应该会得到一个Microsoft.Data.SqlClient.SqlException,因为你的SQL无效。命令应该是 SELECT TOP n * FROM,其中n 是整数值,或者 SELECT TOP n [ColumnName1], [ColumnName2] ... FROM。
你的错误不同,因为你似乎写了自己的SqlCommand类,在其中你把ExecuteReader声明为一个只读属性并抛出Not Implemented。请删除这个类,改用SqlClient中的实现。具体如下:
public sealed class SqlCommand : DbCommand, ICloneable {
...
new public SqlDataReader ExecuteReader() {
SqlStatistics statistics = null;
IntPtr hscp;
Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteReader|API> %d#", ObjectID);
Bid.CorrelationTrace("<sc.SqlCommand.ExecuteReader|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
try {
statistics = SqlStatistics.StartTimer(Statistics);
return ExecuteReader(CommandBehavior.Default, ADP.ExecuteReader);
}
finally {
SqlStatistics.StopTimer(statistics);
Bid.ScopeLeave(ref hscp);
}
}
...
}
如需详细信息,可以在此处查看源代码 here,或者在此处查看类描述 here
sealed 相当于VB.Net中的 NotInheritable。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。