如何使用 SQLSERVERSMO 的 com 库?
我创建了一个 com 库,通过它我可以在 VS2005 中使用 SMo 命名空间发现 SQLVolume。 现在我想在VS2003中使用这个com库来在安装了vs2003的另一台机器上发现相同的sqlvolumes。
我可以这样使用它吗? 我已经使用 VS2005 版本创建了一个 COM,现在我想在 VS2003 中使用它,这可能吗?
我请求你们不要将此标记为重复,因为我需要有效的答案。
using System;
using System.Data.SqlClient;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data;
public interface ICalculator
{
void Show();
};
namespace ManagedDLL
{
public class ManagedClass : ICalculator
{
public void Show()
{
Console.WriteLine("from managed class");
Console.WriteLine("c# "); Console.WriteLine("c#");
DataTable dt = new DataTable();
string srvname,filepath;
//evaluates local instances of SQLSERVER
dt = SmoApplication.EnumAvailableSqlServers(true);
Console.WriteLine("Local Instances are \n");
foreach (DataRow dr in dt.Rows)
Console.WriteLine(dr["Name"]);
foreach (DataRow dr in dt.Rows)
{
srvname = (string)dr["name"];
Console.WriteLine("\nConnecting to Server " + srvname + "\n\nDiscovering Volumes...\n\nDiscovering Sql2005 Volumes for the Host " + srvname.ToLower() + "\n");
Console.WriteLine("\nHostName : " + srvname.ToLower() + "\n");
Server srv = new Server(srvname);
Console.WriteLine(" Server Name \n" + srv.Name);
foreach( Database db in srv.Databases)
{
filepath = db.PrimaryFilePath;
Console.WriteLine("File Path : " + filepath + "\\" + db.Name + ".mdf \n");
Console.WriteLine("File Path : " + filepath + "\\" + db.Name + ".ldf \n");
}
}//foreach
}
}
}
我为这个类创建了一个com库,我必须在vs2003中使用这个com库,还有一件事是smo命名空间。
.net f/w 1.1 不支持上述代码。 我怎样才能这样做?
在使用时,我收到以下错误:
Comssss.exe 中 0x7c812a6b 处未处理的异常:Microsoft C++ 异常:_com_error @ 0x0012fcbc。
I have created a com library by which I can discover the SQLVolumes using SMo namespace in VS2005.
Now I want to use this com library in VS2003 to discover the same sqlvolumes in another machine where vs2003 installed.
Can I use it like this? I have created a COM using version VS2005 and now i want to consume it in VS2003 is it possible?
I request you people not to mark this as a duplicate because I need effective answer.
using System;
using System.Data.SqlClient;
using Microsoft.Win32;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data;
public interface ICalculator
{
void Show();
};
namespace ManagedDLL
{
public class ManagedClass : ICalculator
{
public void Show()
{
Console.WriteLine("from managed class");
Console.WriteLine("c# "); Console.WriteLine("c#");
DataTable dt = new DataTable();
string srvname,filepath;
//evaluates local instances of SQLSERVER
dt = SmoApplication.EnumAvailableSqlServers(true);
Console.WriteLine("Local Instances are \n");
foreach (DataRow dr in dt.Rows)
Console.WriteLine(dr["Name"]);
foreach (DataRow dr in dt.Rows)
{
srvname = (string)dr["name"];
Console.WriteLine("\nConnecting to Server " + srvname + "\n\nDiscovering Volumes...\n\nDiscovering Sql2005 Volumes for the Host " + srvname.ToLower() + "\n");
Console.WriteLine("\nHostName : " + srvname.ToLower() + "\n");
Server srv = new Server(srvname);
Console.WriteLine(" Server Name \n" + srv.Name);
foreach( Database db in srv.Databases)
{
filepath = db.PrimaryFilePath;
Console.WriteLine("File Path : " + filepath + "\\" + db.Name + ".mdf \n");
Console.WriteLine("File Path : " + filepath + "\\" + db.Name + ".ldf \n");
}
}//foreach
}
}
}
I have created a com library for this class, and I have to use this com library in vs2003 and one more thing is the smo namespace.
The above code is not supported by .net f/w 1.1. How to can i do like this?
While consuming I am getting the following error:
Unhandled exception at 0x7c812a6b in Comssss.exe: Microsoft C++ exception: _com_error @ 0x0012fcbc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果您安装了 .net 2 运行时,VS2003 COM 互操作应该可以正常工作。
您在执行此操作时遇到具体问题吗? 如果是这样,您能告诉我们您遇到的问题吗?
Yes, if you have the .net 2 runtimes installed, VS2003 COM interop should work fine.
Are you having a specific problem doing this? If so can you tell us the problem you're experiencing?
您无法将两个版本的框架加载到同一进程中。
VS2003 已经在使用 .NET 1.1,因此您将无法加载需要 .NET 2.0 的进程内 COM 组件。
您也许可以使用 DCOM 并将其置于进程外。
You can't load two versions of the framework into the same process.
VS2003 is already using .NET 1.1, so you won't be able to load an in-proc COM component that needs .NET 2.0.
You may be able to use DCOM and have it out-proc.