VB.NET& SQL Server数据库:从文本框添加记录

时间:2015-04-27 16:50:23

标签: sql sql-server database vb.net

我正在尝试创建一个高分表,它将从文本框中获取用户名,并从另一个表单上的标签中获取分数,并将它们存储到名为highscores的数据库中。我可以得到连接工作但我无法弄清楚如何添加我需要添加的记录并在datagrid视图中显示它们。

    Private MyDatAdp As New SqlDataAdapter
    Private MyCmdBld As New SqlCommandBuilder
    Private MyDataTbl As New DataTable
    Private MyCn As New SqlConnection
    Private MyRowPosition As Integer = 0

    Private Sub highscore_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MyCn.ConnectionString = "Data Source=(localDB)\v11.0;AttachDbFilename=C:\Users\Scores.mdf;integrated security = true;Connect Timeout = 30"
        MyCn.Open()

        MyDatAdp = New SqlDataAdapter("Select* from HighScores", MyCn)
        MyCmdBld = New SqlCommandBuilder(MyDatAdp)
        MyDatAdp.Fill(MyDataTbl)

        Dim MyDataRow As DataRow = MyDataTbl.Rows(1)
        Dim MyDataRow2 As DataRow = MyDataTbl.Rows(2)

        Dim Name As String
        Dim Score As String
        Name = MyDataRow("Name")
        Score = MyDataRow2("Score")
        NameBox.Text = Name.ToString
        Math.Score.Text = Score.ToString
        Me.showRecords()

    End Sub

    Private Sub showRecords()

        If MyDataTbl.Rows.Count = 0 Then
            NameBox.Text = ""
            Math.Score.Text = ""
            Exit Sub
        End If

        NameBox.Text = MyDataTbl.Rows(MyRowPosition)("Name").ToString
        Math.Score.Text = MyDataTbl.Rows(MyRowPosition)("Score").ToString

    End Sub

End Class

0 个答案:

没有答案