在vba excel中执行参数化插入查询
这种新的,我试图执行插入查询(到oracle数据库)与参数化查询在VBBA Excel中
Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Dim ccmd As New ADODB.Command str = "Provider=MSDAORA;Data Source=db;Persist Security Info=True;Password=pword;User ID=uname" Set cnn = CreateObject("ADODB.Connection") cnn.Open str Set rs = CreateObject("ADODB.Recordset") ccmd.ActiveConnection = cnn ccmd.CommandText = "Insert into Table Values(@col1,@col5,@col8,@col6,@col7,@col2,@col3,@col4)" ccmd.CommandType = adCmdText ccmd.Parameters.Append ccmd.CreateParameter("@col1", adVarChar, adParamInput, 50, Cells(i, 1).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col5", adVarChar, adParamInput, 50, Cells(i, 5).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col8", adVarChar, adParamInput, 50, Cells(i, 8).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col6", adVarChar, adParamInput, 50, Cells(i, 6).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col7", adVarChar, adParamInput, 50, Cells(i, 7).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col2", adVarChar, adParamInput, 50, Cells(i, 2).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col3", adVarChar, adParamInput, 50, Cells(i, 3).Value) ccmd.Parameters.Append ccmd.CreateParameter("@col4", adVarChar, adParamInput, 50, Cells(i, 4).Value) 'execute the command here, im having an error here. I'm not sure how to execute the command. I'm also not sure whether the error i'm getting is caused by how im executing the command or something else. 'I've tried: 'ccmd.Execute 'cnn.Execute(ccmd.CommandText) 'rs = ccmd.execute
自动化错误
是我得到的
编辑:
尝试改变我的查询Insert into Table Values(?,?,?,?,?,?,?,?)
,我仍然得到自动化错误。 还尝试删除我的参数名称中的“@”字符,并尝试使用“:”或“?”。
有一些方法可以在插入中包含范围,但是您确定自己获得了良好的连接吗? 自动化错误是一个相当普遍的错误,可以包含几个不同的可能的问题(用户没有正确的权限是常见的),你可以检查下面的代码(请参阅打开后的If)。
如果这样做的话,下一步就是更新你的问题或评论以及新的问题,并且你可以通过各种方法为Oracle添加单元格范围。 此外, MSDAORA贬值,看到更多的信息在这里接受的答案 。
' Works on one of my test systems Dim SQLString As String str = "Provider=MSDAORA;Data Source=db;Persist Security Info=True;Password=pword;User ID=uname" Set cnn = New ADODB.Connection cnn.ConnectionString = str cnn.ConnectionTimeout = 90 ' Set the connection string directly and set timeout rather than open so ' We can check if connect worked. cnn.Open If cnn.State = adStateOpen Then MsgBox "Connected" Else MsgBox "Sorry. Connection Failed" Exit Sub End If ' Then I'd try a simple insert command and see if that works (to target error) ' If this works then the error is likely how the insert is created SqlString = "Insert into table Values('Something Silly')" cnn.Execute SqlString, ,adCmdText ' As this is a direct SQL you shouldn't need adCmdText but will later cnn.Close
- 编写multidimensional array来优秀vba
- Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute未定义
- 如何粘贴在Excel中的列B的最后一行?
- “Microsoft Office 2007主互操作程序集”与“2007 Office System驱动程序:数据连接组件”是一样的吗?
- 检索UsedRange而不使用空白单元格
- 从string中提取数字集,并在新string中以逗号分隔的值返回
- 错误“91”对象variables或块variables未设置
- 从excel中提取数据用于VBA抓取中的input循环
- Javascript / JQuery导出到Internet Explorer 10以上的Excel