数据集到数据库中的表

时间:2016-05-02 16:54:23

标签: vb.net dataset

我有一个名为'ds'的数据集,我想将该数据集放入我的sql数据库中的一个表中。我似乎找不到解决方案。我如何将数据集放入表中?任何帮助将不胜感激!

到目前为止

代码。根据用户输入创建的数据集:

    Dim ds As DataSet = CreateNewDataSet()
    Dim entries As New List(Of String())

    Console.WriteLine("Enter first name: ")
    firstName = Console.ReadLine()
    Console.WriteLine("Enter last name: ")
    lastName = Console.ReadLine()


    entries.Add({firstName, lastName})
    entries.ForEach(Sub(x) ds.Tables(0).Rows.Add(x))

我只使用一个表,'Person',字段'firstname'和'lastname'。

尝试将数据集插入表Person:

     Using connection = New OleDbConnection("connectionstring")

        connection.Open()

        Dim adapter = New OleDbDataAdapter()
        Dim myQuery As String = String.Empty

        myQuery &= "INSERT INTO Applicant (strFirstName, strLastName)"
        myQuery &= "VALUES(?, ?)"

        adapter.InsertCommand = New OleDbCommand(myQuery, connection)
        adapter.InsertCommand.Parameters.AddWithValue("@strFirstName", firstName)
        adapter.InsertCommand.Parameters.AddWithValue("@strLastName", lastName)
        adapter.Update(ds)

    End Using

1 个答案:

答案 0 :(得分:0)

这是解决方案:

Private Function CreateObject(ds as DataSet)

Dim connection as OdbcConnection = new OdbcConnection(conString)
connection.Open()

Dim adapter as OdbcDataAdapter = new OdbcDataAdapter("select * from Table", connection)

ds = new DataSet("DefaultName")
adapter.FillSchema(ds, SchemaType.Source, "Table")
adapter.Fill(ds, "Table")

Dim table as DataTable
table = ds.Tables("Table")

Dim row as DataRow = Nothing
row = table.NewRow()
row("field1") = varField1
row("field2") = varField2
table.Rows.Add(row)

Return ds

End Function