【VB.NET】向SQL Sever写入数据,先设置对应Datatable

在使用VB.NET向SQL Server写入数据时,可能会遇到'項目名’不属于表格的错误。这通常是由于项目名拼写错误或DataTable未正确设置导致。为避免这类问题,可以在Form_load事件中初始化DataTable,并确保在赋值DataRow之前进行设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候会出现报错「'項目名’はテーブルに属していません」
排查原因:
①项目名写错>>>多用复制粘贴。手打易错。
②忘记设置DT>>>不写在Form_load处,易忘。写在DataRow赋值前,肯定不会忘。

例:

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

        Dim tt As String
        Dim tt2 As Date
        Dim ii As Integer
        If MsgBox("予約しますか", vbYesNo, "") = vbYes Then

            Try
                CM.CommandText = "Select * from dbo.reserves"
                DA.SelectCommand = CM
                DA.Fill(DS, "reserves")
                DT = DS.Tables("reserves")

                DR = DT.NewRow()
                tt = Format(Now(), "yyMMddHHmmss")
                ii = DataGridView1.CurrentRow.Index
                tt2 = DataGridView1.Rows(ii).Cells(1).Value

                DR("ReserveID") = tt + dd
                DR("EventDate") = tt2
                DR("EventTime") = DataGridView1.Rows(ii).Cells(2).Value.ToString()
                DR("EventRoom") = DataGridView1.Rows(ii).Cells(3).Value
                DR("EventName") = DataGridView1.Rows(ii).Cells(4).Value.ToString()
                DR("Applicant") = L + " " + F

                DT.Rows.Add(DR)

                CM.CommandText = "insert into dbo.reserves values (" &
               "'" & DR("ReserveID") &
               "',CONVERT(DATETIME, '" & DR("EventDate").ToString & "',111" &
               "),'" + DR("EventTime") &
               "','" & DR("EventRoom") &
               "','" & DR("EventName") &
               "','" & DR("Applicant") &
               "')"
                CM.ExecuteNonQuery()
                MsgBox("予約できました")
                reserve_list.Show()

            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                If DRR.IsClosed = False Then
                    DRR.Close()
                End If
            End Try

            CN.Close()

        Else
            MsgBox("予約できませんでした")
 
        End If

    End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值