SELECT * INTO语句出现问题 [英] Problem with SELECT * INTO statement
问题描述
下面是我到目前为止将Table1复制到同一MS Access数据库中的新Table2的VB.Net代码。但是,我收到错误消息,其中包含以下行: dbConFromDatabase.Executenonquery strSQL ,消息为 ExecuteNonQuery不是System.Data.OleDbConnection 的成员。我是VB.Net的新手。为了执行SQL SELECT * INTO ...语句,我需要什么语句?有人可以帮我纠正这个代码,以便它可以运行没有错误?我会永远很棒!
Below is the VB.Net code I have so far to copy Table1 to a new Table2 within the same MS Access database. However, I get the error message with the line that says: dbConFromDatabase.Executenonquery strSQL, and the message is ExecuteNonQuery is not a member of System.Data.OleDbConnection. I am new to VB.Net. What is the statement that I need in order to execute the SQL SELECT * INTO... statement? Can someone help me correct this code so that it will run without an error? I will be eternally greatful!
Module Module1
Private Sub CopyTable(strFromDatabase As String, strTableName As String, strToDatabase As String)
Dim strSQL As String
Dim dbConFromDatabase As OleDb.OleDbConnection
Try
'open connection from database
dbConFromDatabase = New OleDb.OleDbConnection
dbConFromDatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strToDatabase & ";Persist Security Info=False"
dbConFromDatabase.Open()
strSQL = "SELECT * INTO " & strTableName & " FROM " & strTableName & " IN '" & strFromDatabase & "';"
dbConFromDatabase.Executenonquery strSQL
dbConFromDatabase.Close()
dbConFromDatabase = Nothing
Catch ex As Exception
End Try
End Sub
End Module
推荐答案
您需要创建一个OleDBCommand
[ ^ ]首先。
You need to create anOleDBCommand
[^] first.
dbConFromDatabase.Open();
OleDbCommand command = new OleDbCommand(strSQL, dbConFromDatabase);
command.ExecuteNonQuery();
感谢您的回复Abhinav。
我改变了你在帖子中指出的VB.Net代码 - 见下文。但是,我在 OleDbCommand.command = New OleDbCommand(strSQL,dbConFromDatabase)下得到了波浪线,并且错误消息显示:Command不是System.Data.OleDbCommand的成员。
语句下面还有一条曲线: Command.ExecuteNonQuery ,错误信息显示:ExecuteNonQuery不是串。我还在模块顶部添加了 Imports System.Data.OleDb 。
我在这里做错了什么?我肯定想知道这一点。任何帮助将不胜感激。
Thank you for your response Abhinav.
I changed my VB.Net code as you indicated in your post - see below. However, I get squiggly lines under the OleDbCommand.command = New OleDbCommand(strSQL, dbConFromDatabase) and the error message says: Command is not a member of System.Data.OleDbCommand.
There is also a squiggly line under the statement that says: Command.ExecuteNonQuery and the error message says: ExecuteNonQuery is not a member of String. I also added Imports System.Data.OleDb at the top of the module.
What am I doing wrong here? I would sure like to figure this out. Any help will be greatly appreciated.
Imports System.Data.OleDb
Module Module1
Private Sub CopyTable(strFromDatabase As String, strTableName As String, strToDatabase As String)
Dim strSQL As String
Dim dbConFromDatabase As OleDb.OleDbConnection
Try
'open connection from database
dbConFromDatabase = New OleDb.OleDbConnection
dbConFromDatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strToDatabase & ";Persist Security Info=False"
dbConFromDatabase.Open()
strSQL = "SELECT * INTO " & strTableName & " FROM " & strTableName & " IN '" & strFromDatabase & "';"
OleDbCommand.Command() = New OleDbCommand(strSQL, dbConFromDatabase)
Command.ExecuteNonQuery()
dbConFromDatabase.Close()
dbConFromDatabase = Nothing
Catch ex As Exception
MsgBox(Err)
End Try
End Sub
End Module
我是这个帖子中的原始海报,在我的上一篇文章中,代码搞砸了,所以我在下面复制了它。我仍然遇到我之前的帖子中所述的问题。非常感谢帮助。
I am the original poster in this thread and in my last post the code got screwed up so I have copied it below. I still have the problems as stated in my prior post. Help will be greatly appreciated.
Imports System.Data.OleDb
Module Module1
Private Sub CopyTable(strFromDatabase As String, strTableName As String, strToDatabase As String)
Dim strSQL As String
Dim dbConFromDatabase As OleDb.OleDbConnection
Try
'open connection from database
dbConFromDatabase = New OleDb.OleDbConnection
dbConFromDatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strToDatabase & ";Persist Security Info=False"
dbConFromDatabase.Open()
strSQL = "SELECT * INTO " & strTableName & " FROM " & strTableName & " IN '" & strFromDatabase & "';"
OleDbCommand.Command() = New OleDbCommand(strSQL, dbConFromDatabase)
Command.ExecuteNonQuery()
dbConFromDatabase.Close()
dbConFromDatabase = Nothing
Catch ex As Exception
MsgBox(Err)
End Try
End Sub
End Module
这篇关于SELECT * INTO语句出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!