1

我有一个问题,我无法预订 Sql 服务器 .mdf 数据库,但我可以读取它。

没有出现错误消息,我已经尝试了我能想到的一切。任何帮助将不胜感激,并在此先感谢!下面是用于插入命令的代码:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim SQLCode As New SQLCode
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim lrd As SqlDataReader
    Dim inscmd As New SqlCommand

    cmd.Parameters.AddWithValue("@StaffInitials", lblStaffInitials.Text.Trim())
    lblStaffInitials.Text = lblStaffInitials.Text
    cmd.Parameters.AddWithValue("@Week", lblWeek.Text.Trim)
    lblWeek.Text = lblWeek.Text
    cmd.Parameters.AddWithValue("@Period", (lblPeriod.Text.Trim))
    lblPeriod.Text = lblPeriod.Text
    cmd.Parameters.AddWithValue("@Day", lblDay.Text.Trim)
    lblDay.Text = lblDay.Text
    cmd.Parameters.AddWithValue("@Subject", lblSubject.Text.Trim)
    lblSubject.Text = lblSubject.Text
    cmd.Parameters.AddWithValue("@YearGroup", lblYear.Text.Trim)
    lblYear.Text = lblYear.Text
    cmd.Parameters.AddWithValue("@NumberOfPupils", (lblNoOfPupils.Text.Trim))
    lblNoOfPupils.Text = lblNoOfPupils.Text
    cmd.Parameters.AddWithValue("@ControlledAssesment", lblControlledAssesment.Text.Trim)
    lblControlledAssesment.Text = lblControlledAssesment.Text
    cmd.Parameters.AddWithValue("@Room", lblRoom.Text.Trim)
    lblRoom.Text = lblRoom.Text
    cmd.Parameters.AddWithValue("@Session", txtSession.Text.Trim)
    txtSession.Text = txtSession.Text

con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("RoomBookingSystem.My.MySettings.Database1ConnectionString1").ConnectionString
    Try
        Using cn As New SqlConnection(con.ConnectionString)
            con.Open()
            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

    Finally
        con.Close()
    End Try
    MsgBox("Your Booking Has Been Made Successfully")
    Clicky = False
    MainViewForm.btnBackToBooking.Visible = False
    FormView.Show()
    Me.Hide()
End Sub
4

2 回答 2

1

当只SqlCommand需要一个时,您有两个不同的 Sql 命令。将参数添加到inscmd而不是cmd.

所以只需删除这一行:

Dim cmd As New SqlCommand

其余的都这样做:

inscmd.Parameters.AddWithValue("@StaffInitials", lblStaffInitials.Text.Trim())
.....
.....

另一件需要注意的事情,你不需要这个:

Using cn As New SqlConnection(con.ConnectionString)

像这样更改您的代码:

Try
 con.Open()
 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()

 Catch ex As Exception
     // use message box to read the error (if there is any). or simply read the
     // ex.Message in debuggin mode.
 Finally
     con.Close()
 End Try

顺便说一句,我看不到您在哪里设置此属性con.ConnectionString。你的连接字符串在哪里?

于 2013-02-06T10:25:13.107 回答
1

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

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

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

你的连接字符串

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

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

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

于 2013-02-09T15:22:28.670 回答