VB.Net - 使用InsertOnSubmit添加新行并将其保存到数据库

时间:2016-03-14 15:57:23

标签: sql .net wpf vb.net

护士表(包含额外数据但不必包含)

字段:ID,名称,已启用

示例数据:1 - 测试护士 - 真

我的问题:用户点击添加护士按钮,新的护士被添加到列表中,ID将根据表格中的最后一个ID自动确定;但是,这在这个特定的块中不起作用,尽管这个功能在同一个程序的其他部分也有效。

与问题相关的代码:

护士班

Partial Public Class Nurse

Private Sub Nurse_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
    Using database As New MaternityDatabaseDataContext
        Select Case e.PropertyName
            Case "First_Name"
                If Me.First_Name.Trim <> "" Then
                    database.ExecuteCommand("UPDATE Nurses SET First_Name = {0} WHERE TableID = {1}", Me.First_Name, Me.TableID)
                End If
            Case "Last_Name"
                If Me.Last_Name.Trim <> "" Then
                    database.ExecuteCommand("UPDATE Nurses SET Last_Name = {0} WHERE TableID = {1}", Me.Last_Name, Me.TableID)
                End If
            Case "Display_Name"
                If Me.Display_Name.Trim <> "" Then
                    database.ExecuteCommand("UPDATE Nurses SET Display_Name = {0} WHERE TableID = {1}", Me.Display_Name, Me.TableID)
                End If
            Case "Enabled"
                database.ExecuteCommand("UPDATE Nurses SET Enabled = {0} WHERE TableID = {1}", Me.Enabled, Me.TableID)
            Case "HomeNumber"
                If Me.HomeNumber.Trim <> "" Then
                    database.ExecuteCommand("UPDATE Nurses SET HomeNumber = {0} WHERE TableID = {1}", Me.HomeNumber, Me.TableID)
                End If
            Case "CellNumber"
                If Me.CellNumber.Trim <> "" Then
                    database.ExecuteCommand("UPDATE Nurses SET CellNumber = {0} WHERE TableID = {1}", Me.CellNumber, Me.TableID)
                End If
        End Select
    End Using
End Sub

End Class

包含addButton

的护士用户控件的代码
Imports System.Collections.ObjectModel

Public Class NursesUserControl

Private NurseList As ObservableCollection(Of Nurse)

Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    NurseList = New ObservableCollection(Of Nurse)
    NursesDataGrid.ItemsSource = NurseList
End Sub

Private Sub AddButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles AddButton.Click
    Dim employee As New Nurse
    employee.Enabled = True
    NurseList.Add(employee)

    'TODO: Change using statement below to use maternity brain.
    Using database As New MaternityDatabaseDataContext
        database.Nurses.InsertOnSubmit(employee)
        database.SubmitChanges()
    End Using
End Sub

Private Sub DisableButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles DisableButton.Click
    Dim selectedRow As Nurse = TryCast(NursesDataGrid.SelectedValue, Nurse)
    If selectedRow IsNot Nothing Then
        selectedRow.Enabled = False
    End If
End Sub

Private Sub EnableButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles EnableButton.Click
    Dim selectedRow As Nurse = TryCast(NursesDataGrid.SelectedValue, Nurse)
    If selectedRow IsNot Nothing Then
        selectedRow.Enabled = True
    End If
End Sub

Private Sub NursesDataGrid_CellEditEnding(sender As Object, e As System.Windows.Controls.DataGridCellEditEndingEventArgs) Handles NursesDataGrid.CellEditEnding
    ' Force update on datagrid cell changes instead of just row changes.
    Static isManualInit As Boolean = True
    If e.EditAction = DataGridEditAction.Commit And isManualInit = True Then
        isManualInit = False
        NursesDataGrid.CommitEdit(DataGridEditingUnit.Row, True)
        isManualInit = True
    End If
End Sub

Private Sub NursesUserControl_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    'TODO: Change using statement below to use maternity brain.
    Using database As New MaternityDatabaseDataContext
        For Each employee As Nurse In database.Nurses.OrderBy(Function(Nurse) (Nurse.Last_Name))
            If employee.Enabled = True Then
                NurseList.Add(employee)
            End If
        Next
    End Using
End Sub

End Class

我的错误:无法将空值插入护士表中的列表ID。 为什么不根据护士表中的最后一个条目自动查找TableID?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

检查您的桌面设计。听起来ID没有被设置为身份。

如果可以,请删除表格并使用ID INT IDENTITY(1, 1)重新创建表格。

如果您不能删除它,则必须手动获取代码中的下一个ID。

您可以获得有关身份here

的更多信息