[英]ADODB Recordset Does Not Add New Records In Temporary Table in MS SQL
I want to create a temporary table with two fields "ID" and "TempData".我想创建一个包含两个字段“ID”和“TempData”的临时表。 Then wants to insert data into that table from an excel sheet through looping.
然后想通过循环将数据从 excel 表中插入到该表中。 I created temporary table but when I try to add data into that table then it fails.
我创建了临时表,但是当我尝试将数据添加到该表中时,它失败了。 Does anyone have idea to move an excel column values to a temporary table without looping?
有没有人有想法将 excel 列值移动到临时表而不循环? Can anyone please help.
任何人都可以请帮忙。
Sub MPNTEST()
Dim dbCon As ADODB.Connection, dbt As ADODB.Recordset, dbtf As ADODB.Field, r As Range, SqlQry1 As String
Set dbCon = New ADODB.Connection
Set dbt = New ADODB.Recordset
dbCon.ConnectionString = SqlConStr
On Error GoTo CloseConnection
SqlQry1 = "Use tempdb CREATE TABLE #TempTbl ( ID INT Primary KEY IDENTITY(1,1),TempData NVARCHAR(255))"
dbCon.Open
dbCon.Execute (SqlQry1)
With dbt
.ActiveConnection = dbCon
.Source = "#TempTbl"
.LockType = adLockOptimistic
.CursorType = adOpenForwardOnly
.Open
For Each r In ActiveSheet.Range("AE2:AE10")
.AddNew
.Fields("TempData").Value = r.Value
.Update '###Here it fails
Next r
End With
dbt.Close
dbCon.Close
End Sub
Hope this help (I have not tested:希望这会有所帮助(我还没有测试过:
Sub MPNTEST()
Dim dbCon As ADODB.Connection, dbt As ADODB.Recordset
Dim dbtf As ADODB.Field, r As Range, SqlQry1 As String
Dim SQLInsert$
Set dbCon = New ADODB.Connection
'Set dbt = New ADODB.Recordset
dbCon.ConnectionString = SqlConStr
On Error GoTo CloseConnection
''create table db..table
SqlQry1 = "CREATE TABLE tempdb..#TempTbl ( ID INT Primary KEY IDENTITY(1,1),TempData NVARCHAR(255))"
dbCon.Open
dbCon.Execute (SqlQry1)
dbCon.Execute "use tempdb" ' Move to tempdb
With dbt
For Each r In ActiveSheet.Range("AE2:AE10")
SQLInsert = "Insert into #TempTbl(TempData) values('" & Replace(""& r.Value, "'", "''") &"')"
dbCon.Execute (SQLInsert)
Next r
End With
'dbt.Close
dbCon.Close
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.