VB.Net表单应用程序数据库表更新

时间:2018-11-07 08:54:51

标签: sql-server vb.net visual-studio-2015

我正在尝试制作一个小型应用程序,允许用户读取描述仓库清单的表的内容,根据2行进行搜索,该行指示该项目位于哪个仓库中,并通过分配的条形码我已经设法获取了该条形码可以通过使用绑定源和数据网格视图来工作,通过查询将条形码和位置作为两个框中的字符串的字符串来更新视图。

此应用程序需要满足我的基本目标的第二部分是拥有一种添加新行并将其存储到数据库中原始表中的方法,以便用户可以独立于仓库直接添加新项目。

到目前为止,我遇到了2个问题:我需要一个表示顺序ID的主键,但是我不知道如何产生顺序递增的ID,我设法使用{{1}获取第一个附加ID }查询组合,但是在添加新行后数据不会更新,这会产生错误,因为它尝试为主键添加具有相同值的另一行。 我遇到的第二个问题是:gridview会根据我在文本框中输入的数据进行相应更改,这些文本是我设置的,以收集表的各种值,但是数据库本身的表未显示任何更改,仅保留测试数据我是在创建时输入的。

top 1 order by desc

总结一下:

  • 我将如何更新数据库表以包括新行?
  • 有没有一种聪明的方法来找到最后一个值,将其增加1以得到下一个值,并在插入新行时对其进行更新,以免最后以相同主键值的2行结尾,生成错误?

1 个答案:

答案 0 :(得分:0)

要在Db中设置增量ID,假设您有权访问SQL Server Management Studio,请在表的“设计”中为“ ID”列的“列属性”中,向下滚动到“身份规范”,然后将(“身份”)设置为“是”

要添加新行,请使用以下代码:

Using NotesDS As New DataSet
    Using NotesDA As New SqlDataAdapter With {.SelectCommand = New SqlCommand With {.Connection = SQLDBConnection, .CommandText = "SELECT * FROM Notes WHERE ID = " & ID}}
        NotesDA.Fill(NotesDS, "Notes")
        Using NotesDV As New DataView(NotesDS.Tables("Notes"))
            Using NoteBuilder As New SqlCommandBuilder(NotesDA) With {.QuotePrefix = "[", .QuoteSuffix = "]"}                        
                If NotesDV.Count = 0 Then                             
                    Dim NoteDRV As DataRowView = NotesDV.AddNew                       
                    NoteDRV.Item("UserName") = UserName
                    NoteDRV.Item("Note") = Note
                    NoteDRV.Item("NoteDate") = NoteDate
                    NoteDRV.Item("CompanyCode") = CompanyCode
                    NoteDRV.EndEdit()
                    NotesDA.UpdateCommand = NoteBuilder.GetUpdateCommand
                    NotesDA.Update(NotesDS, "Notes")
                End If
            End Using
        End Using
    End Using
End Using

显然,请修改为适合您的表名和列名。

如果您需要检索ID以进行显示,则可以向Update添加处理程序,例如:

Public Sub GenericOnRowUpdated(sender As Object, e As System.Data.SqlClient.SqlRowUpdatedEventArgs)        
    Dim newID As Integer = 0
    Dim idCMD As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT @@IDENTITY", SQLDBConnection)
    If e.StatementType = StatementType.Insert Then            
        newID = CInt(idCMD.ExecuteScalar())
        e.Row("ID") = newID
    End If
End Sub

并像这样使用:

 AddHandler NotesDA.RowUpdated, New SqlRowUpdatedEventHandler(AddressOf GenericOnRowUpdated)
 NotesDA.Update(NotesDS, "Notes")
 NewID = NoteDRV.Item("ID")

编辑

第一个示例已修改并在下面说明:

'Declare you connection to the SQL dB. Connection String looks like "Data Source=192.168.71.10\dBName; Initial Catalog=dBName; User ID=USER; Password='PASSWORD!';MultipleActiveResultSets=true"  -  You may well already have an open connection, and can use that instead. Not sure what your 
StockBindingSource is...
    Dim oConn As New SqlConnection("CONNECTION STRING")
    'Open the connection
    oConn.Open()
    'Declare Your DataAdapter and initialise using your connection
    Dim DA As New SqlDataAdapter With {.SelectCommand = New SqlCommand With {.Connection = oConn, .CommandText = "SELECT * FROM Stock WHERE ID=0"}}
    'Declare you DataSet
    Dim DS As New DataSet
    'Fill Your DataSet with the Stock table from your DataAdapter
    DA.Fill(DS, "Stock")
    'Declare a DataView for easy use (really the same as using DS.Tables("Stock").DefaultView)
    Dim DV As New DataView(DS.Tables("Stock"))
    'Declare a CommandBuilder and initialise with your DataAdapter. This will now watch for changes made to your data and build the appropriate SQL UPDATE/INSERT/DELETE command. the "[" and "]" are in case any column names use reserved words
    Dim Builder As New SqlCommandBuilder(DA) With {.QuotePrefix = "[", .QuoteSuffix = "]"}
    'Decalre a DataRowView for data population, based on your DataView table structure
    Dim R As DataRowView = DV.AddNew()
    'Populate the fileds with your Form data
    R("Supplier") = Supplier.Text
    R("Producer_code") = ProducerCode.Text
    R("Barcode") = Barcode.Text
    R("Comp_name") = ComponentName.Text
    R("Warehouse") = Warehouse.Text
    R("Internal_Code") = InternalCode.Text
    R("Description_IT") = ITDescr.Text
    R("Description_EN") = ENDescr.Text
    R("Quantity") = "0"
    'Notify that the edit has finished
    R.EndEdit()
    'Get the SQL command from the CommandBuilder 
    DA.UpdateCommand = Builder.GetUpdateCommand()
    'Execute the update (in this case it will be an INSERT) 
    DA.Update(DS, "Stock")