gpt4 book ai didi

sql - 数据库会读但不会写

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:28 24 4
gpt4 key购买 nike

我遇到一个问题,我无法对 Sql server .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

最佳答案

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

当程序被调试时,一个数据库的副本被放置在 bin/debug 文件夹中,如果你在这里查看,你会看到副本,你的代码将有权访问这个数据库和不是原始数据库 (如果您单击“显示所有文件”然后转到 bin/调试文件,然后使用数据库资源管理器打开,您可以看到上次调试/运行时估算的数据)

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

你的连接字符串

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

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

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

关于sql - 数据库会读但不会写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com