简体   繁体   中英

ADO.NET with VB.NET - SELECT INTO query

I have been successful with setting up a connection and retrieving information from my MS Access database to my Windows Form in Visual Studio using VB.NET and SELECT statements with some of the sample code below.

    Dim dataset As New DataSet

    Dim datatable As New DataTable
    dataset.Tables.Add(datatable)

    Dim data_adaptor As OleDbDataAdapter
    data_adaptor = New OleDbDataAdapter("SELECT * FROM test1", odbconnect)

    data_adaptor.Fill(datatable)

However when I set my DataAdaptor to a query like below, I have run into some trouble executing a SELECT INTO query.

    data_adaptor = New OleDbDataAdapter("SELECT first_name INTO newtable FROM test1", odbconnect)  

I do not get any error but the table does not get created in the my access database. I understand that I would have to redeclare or declare a new, Data Adaptor to actually get information from the newly created "newtable" but if I execute this, then open my Access Database, "newtable" does not exists. However, if I run this query in Access, it works but I get a pop up box confirming that I want to paste data into a new table. I imagine this could be a possible hang up with working with MS Access in this manner. Is there some other setting or object I must use to execute a SELECT INTO query to create a new table?

Do not use a OleDbDataAdapter for this. Use simply a OleDbCommand and call ExecuteNonQuery

dbCmd = New OleDbCommand("SELECT first_name INTO newtable FROM test1", odbconnect)
cbCmd.ExecuteNonQuery()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM