【发布时间】:2013-12-22 19:26:38
【问题描述】:
如果我在这里忽略了一些非常明显的东西,请原谅我,因为我对编程完全陌生。在过去的几天里,我一直在尝试解决我一直遇到的问题。长话短说,我正在构建一个 CRM 应用程序,其中包括一个包含四个表(员工、响应、状态和表)的数据库。我相信我的问题与我的连接字符串有关,但我不确定如何解决它。根据数据库属性窗口,连接字符串如下:
Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf";Integrated Security=True
我目前收到以下错误:
在 349591.exe 中发生了“System.InvalidOperationException”类型的未处理异常
附加信息:创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:在预期条件的上下文中指定的非布尔类型的表达式,靠近 ','。
我已经尝试了我所知道的一切,但我很难过。感谢您提供任何帮助。请在下面找到我写的 DBUtil 代码:
Imports System.Data
Imports System.Data.SqlClient
Public Class DButil
Public cs As String
Public Function GetDataView(ByVal sql As String) As DataView
Dim ds As New DataSet
Dim da As New SqlDataAdapter(sql, cs)
da.Fill(ds)
Dim dv As New DataView(ds.Tables(0))
Return dv
End Function
Public Sub New()
'cs = "Data Source=(LocalDB)\v11.0;"
'cs = "Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True"
'cs += "Integrated Security =True;"
' Dim strPath As String = Replace(System.AppDomain.CurrentDomain.BaseDirectory, "bin\debug\", "cms.mdf")
cs = ("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True")
End Sub
Public Function SaveComplaint(
ByVal ComplaintID As Integer,
ByVal Description As String,
ByVal Proposal As String,
ByVal Location As String,
ByVal OpenDate As Object,
ByVal CloseDate As Object,
ByVal StatusID As Integer,
ByVal EmployeeID As Integer) As Boolean
OpenDate = Convert.ToDateTime(OpenDate)
If CloseDate.ToString.Length = 0 Then
CloseDate = DBNull.Value
Else
CloseDate = Convert.ToDateTime(CloseDate)
End If
Dim sql As String
If ComplaintID = 0 Then
sql = "INSERT INTO Complaints (Description, Proposal, Location, OpenDate, "
sql += "CloseDate, StatusID, EmployeeID) VALUES (@Description, @Proposal, "
sql += "@Location, @OpenDate, @CloseDate, @StatusID, @EmployeeID)"
Else
sql = "UPDATE Complaints SET Description=@Description, Proposal=@Proposal, Location=@Location, OpenDate=@OpenDate, "
sql += "CloseDate=@CloseDate, StatusID=@StatusID, EmployeeID=@EmployeeID WHERE ComplaintID=@ComplaintID"
End If
Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True")
Dim cm As New SqlCommand(sql, cn)
Try
With cm.Parameters
.AddWithValue("@ComplaintID", ComplaintID).DbType = DbType.Int32
.AddWithValue("@Description", Description).DbType = DbType.String
.AddWithValue("@Proposal", Proposal).DbType = DbType.String
.AddWithValue("@Location", Location).DbType = DbType.String
.AddWithValue("@OpenDate", OpenDate).DbType = DbType.DateTime
.AddWithValue("@CloseDate", CloseDate).DbType = DbType.DateTime
.AddWithValue("@StatusId", StatusID).DbType = DbType.Int32
.AddWithValue("@EmployeeID", EmployeeID).DbType = DbType.Int32
End With
cn.Open()
cm.ExecuteNonQuery()
cn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error in DBUtil.SaveComplaint",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Public Function RunSQL(ByVal sql As String) As Boolean
Dim cn As New SqlConnection(cs)
Dim cm As New SqlCommand(sql, cn)
Try
cn.Open()
cm.ExecuteNonQuery()
cn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & sql, "Error in DBUtil.RunSQL", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Class
【问题讨论】:
-
您不能直接连接到 MDF 文件本身。假设您在本地连接,则需要在您的计算机上运行 SQL Server。
-
使用 LocalDB 可以直接连接到 .mdf 文件。
-
仔细检查您的连接字符串。看起来你有两次 Integrated Security=True 。
-
感谢您的回复。我删除了两次的 Integrated Security=True ,但仍然收到相同的错误。再次感谢您的热心帮助!
-
“创建表单时出错” - 怀疑这表明 .designer.vb 文件中的代码有问题。
标签: sql vb.net sql-server-2012-express localdb