我试图在Excel和SQL之间建立链接但是我收到以下错误消息"没有为命令对象设置命令文本"运行VBA代码时。我对VBA很新,有人能看到我出错的地方吗?
仅供参考我已经在Sheet2.Range中复制了SQL代码(" A2")
这是我的VBA代码。
Sub Update()
Dim link As New ADODB.Connection
Dim login As String
Dim SQLSyntax As String
Dim Basket As New ADODB.Recordset
login = "DRIVER=SQL Server Native Client 10.0;SERVER=xxxxxx;UID=sa;PWD=xxxxx;APP=2007 Microsoft Office system;WSID=PC3;DATABASE=ST_L1;"
SQLSyntax = Sheet2.Range("A2").Text
link.Open login
Basket.Open SQLSyntax, link
Basket.Close
link.Close
End Sub
我的SQL代码(在SQL中工作正常)。
Select P.spn as [Number],P.[Group],P.Name, isnull(T.Calls,0) as [Calls in last 2 months (excluding internals)], isnull(L.[Internal Calls],0) as [Internal Calls] ,isnull(M.[Last call],0)as [Last call] from ST_L1.dbo.Numbers P
left join
(Select COUNT(*)as [Calls],SPN from ST_L1.dbo.Main where CLI not in (Select CLI from ST_L1.dbo.internalCLI)
and start between convert (date,getdate()-62 )and convert (date,getdate())
group by SPN)T on T.SPN = P.spn
left join (Select max(start)[Last call],SPN from ST_L1.dbo.Main
group by SPN)M on M.SPN = P.spn
left join
(Select COUNT(*)as [Internal Calls],SPN from ST_L1.dbo.Main where CLI in (Select CLI from ST_L1.dbo.internalCLI)
and start between convert (date,getdate()-62 )and convert (date,getdate())
group by SPN)L on L.SPN = P.spn
where LEFT(P.spn,4) not in ('0870') and P.[Group] in
('BlaBla', 'BlaBla2')
order by T.Calls asc,M.[Last call]
答案 0 :(得分:0)
你永远不会打开记录集吗?
public class PerformanceServiceFactory
{
private static ConcurrentDictionary<Type, ConstructorInfo> Constructors
= new ConcurrentDictionary<Type, ConstructorInfo>();
public static object Create(Type type, PerformanceContext perfContext)
{
ConstructorInfo ctor;
if(Constructors.TryGetValue(type, out ctor))
{
return InvokeConstructor(ctor, perfContext);
}
else if (type.IsSubclassOf(typeof(PerformanceMonitoredService))
||type.IsAssignableFrom(typeof(PerformanceMonitoredService)))
{
ConstructorInfo newCtor = type.GetConstructor(
new[] { typeof(PerformanceContext) }
);
if(Constructors.TryAdd(type, newCtor))
{
return InvokeConstructor(newCtor, perfContext);
} else if(Constructors.TryGetValue(type, out ctor))
{
return InvokeConstructor(ctor, perfContext);
}
}
throw new ArgumentException(
$"Expected type inheritable of {typeof(PerformanceMonitoredService).Name}"}",
"type");
}
private static object InvokeConstructor(ConstructorInfo ctor,
PerformanceContext perfContext)
{
return ctor.Invoke(new object[] { perfContext });
}
}