操作系统:编程环境:VS2013; 语言:VB.net; 数据库:SQLserver2008
做数据库连接时,发生的错误:
错误提示为:
说明:用VB.net连接SQLServer数据库
第一种情况:
连接字符串为“Server=(Local);Database=charge_sys;UserID = sa;Password=123456”时,连接没问题;
第二种情况:
当把“ Server=(Local)”换为 “ Server=192.168.24.123 ”,即把local换为数据库服务器IP地址时,连接不成功,出现如上错误(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值不同。
System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生,布布扣,bubuko.com
System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生
原文:http://blog.csdn.net/huyuyang6688/article/details/28388659