在VB 2008中预订本地数据库的问题

时间:2013-01-25 10:39:42

标签: sql vb.net

您好我正在尝试在本地.mdf数据库中查看有关课程的详细信息,但似乎无法这样做,我已经有几个星期这个问题了,真的需要解决它所以任何帮助将不胜感激。 我将在下面的表单中包含我的代码。 在此先感谢您的帮助!!

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim Session As String
    Session = txtSession.text

    Dim con As New SqlConnection
    Dim inscmd As New SqlCommand
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("RoomBookingSystem.My.MySettings.Database1ConnectionString1").ConnectionString
    Try
        Using cn As New SqlConnection(con.ConnectionString)
            con.Open()

            Dim ParamInitial As New SqlParameter
            ParamInitial.ParameterName = ("@StaffInitials")
            ParamInitial.Value = lblStaffInitials.Text.Trim()
            inscmd.Parameters.Add(ParamInitial)

            Dim ParamWeek As New SqlParameter
            ParamWeek.ParameterName = ("@Week")
            ParamWeek.Value = lblWeek.Text.Trim
            inscmd.Parameters.Add(ParamWeek)

            Dim ParamPeriod As New SqlParameter
            ParamPeriod.ParameterName = ("@Period")
            ParamPeriod.Value = lblPeriod.Text.Trim
            inscmd.Parameters.Add(ParamPeriod)

            Dim ParamDay As New SqlParameter
            ParamDay.ParameterName = ("@Day")
            ParamDay.Value = lblDay.Text.Trim
            inscmd.Parameters.Add(ParamDay)

            Dim ParamSubject As New SqlParameter
            ParamSubject.ParameterName = ("@Subject")
            ParamSubject.Value = lblSubject.Text.Trim
            inscmd.Parameters.Add(ParamSubject)

            Dim ParamYearGroup As New SqlParameter
            ParamYearGroup.ParameterName = ("@YearGroup")
            ParamYearGroup.Value = lblYear.Text.Trim
            inscmd.Parameters.Add(ParamYearGroup)

            Dim ParamNumberPupils As New SqlParameter
            ParamNumberPupils.ParameterName = ("@NumberOfPupils")
            ParamNumberPupils.Value = lblNoOfPupils.Text.Trim
            inscmd.Parameters.Add(ParamNumberPupils)

            Dim ParamControlledAssesment As New SqlParameter
            ParamControlledAssesment.ParameterName = ("@ControlledAssesment")
            ParamControlledAssesment.Value = lblControlledAssesment.Text.Trim
            inscmd.Parameters.Add(ParamControlledAssesment)

            Dim ParamRoom As New SqlParameter
            ParamRoom.ParameterName = ("@Room")
            ParamRoom.Value = lblRoom.Text.Trim
            inscmd.Parameters.Add(ParamRoom)

            Dim ParamSession As New SqlParameter
            ParamSession.ParameterName = ("@Session")
            ParamSession.Value = txtSession.Text.Trim
            inscmd.Parameters.Add(ParamSession)

            inscmd.CommandText = "INSERT INTO Booking (Week, Day, Period, Subject, YearGroup, StaffInitials, NumberOfPupils, Session, Room, ControlledAssesment)VALUES (@Week,@Day,@Period,@Subject,@YearGroup,@StaffInitials,@NumberOfPupils,@Session,@Room,@ControlledAssesment)"
            'inscmd.CommandText = "INSERT INTO Booking (Week, Day, Period, Subject, YearGroup, StaffInitials, NumberOfPupils, Session, Room, ControlledAssesment)VALUES ('B','Monday',5,'Politics','U6','JAGG',5,'5','LIT','False')"
            Print(inscmd.CommandText)
            inscmd.Connection = con
            inscmd.ExecuteNonQuery()
            con.Close()
            inscmd.Parameters.Clear()
        End Using
        ' Catch ex As Exception
        '   MsgBox("" & ex.Message)
        'if there is an error it will go here (can use Msgbox or label)
    Finally
        con.Close()
    End Try
    MsgBox("Your Booking Has Been Made Successfully")
    Clicky = False
    MainViewForm.btnBackToBooking.Visible = False
    FormView.Show()
    Me.Hide()
    con.Close()
End Sub

2 个答案:

答案 0 :(得分:1)

我在上面的示例中看到两个insert命令。注释掉的命令显示两个值被视为数字而不是字符串 您使用使用参数的命令文本,但所有准备好的参数都设置为字符串数据类型(您没有设置任何数据类型,这默认DbType属性为NVarChar) 可能这是执行insert命令时得到的确切错误消息

您可以尝试以下方法:

   Using cn As New SqlConnection(con.ConnectionString)
        con.Open()
        inscmd.Parameters.AddWitValue("@StaffInitials", lblStaffInitials.Text.Trim())
        inscmd.Parameters.AddWithValue("@Week", lblWeek.Text.Trim)
        inscmd.Parameters.AddWithValue("@Period", Convert.ToInt32(lblPeriod.Text.Trim))
        inscmd.Parameters.AddWithValue("@Day",lblDay.Text.Trim)
        inscmd.Parameters.AddWithValue("@Subject", lblSubject.Text.Trim)
        inscmd.Parameters.AddWithValue("@YearGroup", lblYear.Text.Trim)
        inscmd.Parameters.AddWithValue("@NumberOfPupils", Convert.ToInt32(lblNoOfPupils.Text.Trim))
        inscmd.Parameters.AddWithValue("@ControlledAssesment", lblControlledAssesment.Text.Trim)
        inscmd.Parameters.AddWithValue("@Room",lblRoom.Text.Trim)
        inscmd.Parameters.AddWithValue("@Session", txtSession.Text.Trim)

        inscmd.CommandText = "INSERT INTO Booking ([Week], [Day], [Period], [Subject], " +
                             "[YearGroup], [StaffInitials],[NumberOfPupils],[Session], " + 
                             "[Room],[ControlledAssesment]) "+
                             "VALUES " + 
                             "(@Week,@Day,@Period,@Subject,@YearGroup,@StaffInitials," +
                             "@NumberOfPupils,@Session,@Room,@ControlledAssesment)"
        inscmd.Connection = con
        inscmd.ExecuteNonQuery()
        inscmd.Parameters.Clear()
    End Using

我使用AddWithValue方法隐式创建一个带有DbType属性的参数,该属性派生自添加的值。因此,如果我添加一个字符串作为值,该参数将具有DbType.NVarchar,但如果我设置一个整数,则DbType将为DbType.Int。当然,这假设您的所有字段都是NVARChar类型,PeriodNumberOfPupils除外。如果不是这样,您应该将适当的转换添加到参数

编辑:不确定,但我怀疑DAY是一个保留的关键字。最好用方括号封装列名

答案 1 :(得分:1)

据我所知,问题出在数据库位置。

当程序被调试时,数据库副本放在bin / debug文件夹中,如果你在这里查看,你会看到副本,你的代码将直接到这个数据库并且不是原始数据库 (如果单击显示所有文件然后转到bin / debug文件,然后使用数据库浏览器打开,您可以看到上次调试/运行时估算的数据)

要解决此问题,您需要执行以下操作:

您的连接字符串

 con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("RoomBookingSystem.My.MySettings.Database1ConnectionString1").ConnectionString

将连接到前面提到的bin文件夹中的数据库。

您需要进入 app.config 文件,找到数据库的具体位置并将其用于连接字符串,这样您就可以从非拷贝中插入/更新数据数据库中。