0

嗨,我目前正在尝试在本地 .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
4

2 回答 2

1

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

您可以尝试以下方法:

   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 类型,除了Periodand NumberOfPupils。如果不是这样,您应该向参数添加适当的转换

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

于 2013-01-25T11:58:54.583 回答
1

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

当程序被调试时,您的数据库副本被放置在 bin/debug 文件夹中,如果您查看此处,您将看到该副本并且您的代码将直接访问该数据库而不是原始数据库 (如果您单击显示所有文件然后转到 bin/debug 文件,然后使用数据库资源管理器打开,您可以看到上次调试/运行时估算的数据)

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

你的连接字符串

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

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

您需要进入您的app.config文件找到数据库的特定位置并将其用于连接字符串,这将允许您从数据库的非副本中插入/更新数据。

于 2013-02-09T15:26:26.180 回答