錯誤背景:
操作系統:編程環境:VS2013。 語言:VB.net; 數據庫:SQLserver2008
做數據庫連接時。發生的錯誤:
錯誤提示為:
說明:用VB.net連接SQLServer數據庫
第一種情況:
連接字符串為“Server=(Local);Database=charge_sys;UserID = sa;Password=123456”時,連接沒問題;
另外一種情況:
當把“ Server=(Local)”換為 “ Server=192.168.24.123 ”。即把local換為數據庫serverIP地址時,連接不成功。出現如上錯誤(PS:其它人的機器上就能夠)。
第一種情況的代碼:
Public Class UserDAO Public connStr As String = "Server=(Local);Database=charge_sys;User ID = sa;Password=123456" Public Function UserLogin(ByVal userName As String, ByVal password As String) As Boolean <span style="white-space:pre"> </span> Dim conn As New SqlConnection(connStr) Dim dbcmd As New SqlCommand Dim reader As SqlDataReader <span style="white-space:pre"> </span> Dim sqlStr As String = "select * from User_Info where userID=@userName and PWD=@password" '與數據庫建立連接 conn.Open() '操作數據庫 dbcmd = New SqlCommand(sqlStr, conn) dbcmd.Parameters.Add(New SqlParameter("@userName", userName)) dbcmd.Parameters.Add(New SqlParameter("@password", password)) reader = dbcmd.ExecuteReader If reader.Read Then Return True Else Return False End If '關閉與數據庫的連接 conn.Close() End Function End Class另外一種情況的代碼
Public Class UserDAO Public connStr As String = "Server=192.168.24.123;Database=charge_sys;User ID = sa;Password=123456" Public Function UserLogin(ByVal userName As String, ByVal password As String) As Boolean <span> </span> Dim conn As New SqlConnection(connStr) Dim dbcmd As New SqlCommand Dim reader As SqlDataReader <span> </span> Dim sqlStr As String = "select * from User_Info where userID=@userName and PWD=@password" '與數據庫建立連接 conn.Open() '操作數據庫 dbcmd = New SqlCommand(sqlStr, conn) dbcmd.Parameters.Add(New SqlParameter("@userName", userName)) dbcmd.Parameters.Add(New SqlParameter("@password", password)) reader = dbcmd.ExecuteReader If reader.Read Then Return True Else Return False End If '關閉與數據庫的連接 conn.Close() End Function End Class兩種代碼的不同之處只在於數據庫連接字符串中的Server值不同。
解決方法:
第一種方法:
重置winsock,我的系統為Win7 64位旗艦版,用管理員身份執行:netsh winsock reset ,重新啟動,得到解決。
其它系統重置方法或者出現故障,可參考百度百科《netsh winsock reset》。
第一種方法:
更新.NET Framework。
在VisualStudio的反饋中心找到了解決方法:
After installing VS 2013 unable to add data connections? 。在這個帖子以下有這樣一句評論:

在VS中看了一下.NET Framework的版本號:

於是去下載了最新版本號的
.NET Framework4.5.2
(點擊跳到下載鏈接)。安裝之后上述問題“ ‘System.AccessViolationException’ 類型的未經處理的異常在 System.Data.dll 中發生” 得到
完美解決。
總結:
發生此問題“ ‘System.AccessViolationException’ 類型的未經處理的異常在 System.Data.dll 中發生”,可能是安裝了VS2013后對系統中的
winsock接口產生影響。因此第一種方法重置winsock能夠解決
;微軟近期幾天剛公布的.NET Framework4.5.2也徹底攻克了這個問題,因此升級也是個不錯的選擇
。


發生此類問題肯定還有其它原因和解決方式,假設您還有所補充,歡迎您給我回復,互相學習。共同進步。
