SQL Server 2012 Express / VB.NET数据库应用程序问题

时间:2013-12-22 19:26:38

标签: sql vb.net sql-server-2012-express localdb

如果我忽视了一些非常明显的东西,请原谅我,因为我对编程完全不熟悉。我花了最近几天试图解决我遇到的问题。长话短说,我有一个我正在构建的CRM应用程序,其中包括一个包含四个表(Employee,Responses,Status和Table)的数据库。我相信我的问题是我的连接字符串,但我不确定如何解决它。根据数据库属性窗口,连接字符串如下:

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

1 个答案:

答案 0 :(得分:0)

您是否已将绑定设置为cn.Open(),然后逐步执行代码以查看错误发生的位置?