0

データセットを使用してデータベースにレコードを保存したいのですが、データがデータベースにコミットされていません。

私のコードは以下で見ることができます:

Dim mydataset1 As New MyDataSet

Dim row As DataRow = mydataset1.Tables("testtable").NewRow()

With row
    .Item("name") = "Segun Omotayo"
    .Item("address") = "Abuja"
End With

mydataset1.Tables("testtable").Rows.Add(row)

どんな助けでもありがたいです

4

2 に答える 2

1

DataSet/DataTableは、データベースのオフライン/メモリ内表現です。データベースを更新する場合は、を使用する必要がありますDataAdapter

例(MS-Sql-Serverを使用していると仮定):

Public Function UpdateDataSet(dataSet As DataSet) As Int32
    Using con = New SqlConnection(My.Settings.SqlConnection)
        Dim sql = "INSERT INTO TUser(Name,Address)VALUES(@Name,@Address)"
        Using cmd = New SqlCommand(sql, con)
            cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar))
            cmd.Parameters.Add(New SqlParameter("@Address", SqlDbType.VarChar))
            Using da = New SqlDataAdapter()
                da.InsertCommand = cmd
                con.Open()
                Dim rowCount = da.Update(dataSet)
                Return rowCount
            End Using
        End Using
    End Using
End Function
于 2012-09-19T14:23:07.653 に答える
0

VB.NET を作成したり、データ アダプター/データセット/データテーブルを使用したりしてから長い時間が経過しているため、ここで錆びている可能性がありますが、そのルートを選択する場合は、次のようなコードが必要になると思います。

Dim connection As New SqlConnection("#####YourConnectionString#####")
connection.Open()

Dim adapter As New SqlDataAdapter("SELECT * FROM testtable", connection)

' For the line below to work, you must have a primary key field in "testtable"
Dim builder As New SqlCommandBuilder(adapter)

Dim testtable As New DataTable("testtable")

adapter.Fill(testtable)

Dim row As DataRow = testtable.NewRow()

With row
    .Item("name") = "Segun Omotayo"
    .Item("address") = "Abuja"
End With

testtable.Rows.Add(row)

adapter.Update(testtable)

connection.Close()
于 2012-09-19T14:33:42.930 に答える