How to use SQLDMO in C#?

H

Henry Padilla

I am trying to use SQLDMO in C# and it's a nightmare.

Any examples, suggestions, is there a different (managed) way to do this
that I don't know about?

Thanks for the help.
Tom Padilla
 
O

oj

You will need to create an interop for dmo before you can use it. Here are
the steps:

1. add a reference to dmo - browse to sqldmo.dll (normally found under
\tools\binn of sql directory)
2. and here is a simple code to get you started

using System;
using SQLDMO;

namespace dmo2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ListSQLs();
}

public static void ListSQLs()
{
//
// List available SQL Server on subnet
//
SQLServer2 oServer = new SQLServer2();
Application oApp = oServer.Application;
NameList oList = oApp.ListAvailableSQLServers();

for (int i=0; i < oList.Count; i++)
{
Console.WriteLine(oList.Item(i));
}
Console.Read();

} //end ListSQLs
}
}
 
H

Henry Padilla

oj said:
You will need to create an interop for dmo before you can use it. Here are
the steps:

1. add a reference to dmo - browse to sqldmo.dll (normally found under
\tools\binn of sql directory)
2. and here is a simple code to get you started

using System;
using SQLDMO;

namespace dmo2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ListSQLs();
}

public static void ListSQLs()
{
//
// List available SQL Server on subnet
//
SQLServer2 oServer = new SQLServer2();
Application oApp = oServer.Application;
NameList oList = oApp.ListAvailableSQLServers();

for (int i=0; i < oList.Count; i++)
{
Console.WriteLine(oList.Item(i));
}
Console.Read();

} //end ListSQLs
}
}

I appreciate the help and it's my fault for leaving this an open-ended
question. Let me try again.

I am trying to make a mini-Enterprise Manager and so far I can get the
server groups and list the registered servers but when I try to get a
specific server from the SQLServers collection (by name) it doesn't return
anything.

This should be possible, right?

Following your example above I should be able to the following:

SQLServer2 currentSQLServer = oApp.SQLServers("DBName")

But for some reason it comes back a null object.

I have no idea how to continue. (Loop through the list until I find the
SQLServer.ServerName = "DBName"?)

Tom P.
 
H

Henry Padilla

oj said:
You have to connect to the server in order to access its objects.

e.g.
oServer.Connect "myservername","login","password"

Unless I know if this is a secure server how do I connect to it?
But I can't get any info from the Application object regarding a particular
server.

I suggest you take a look at this:
http://msdn.microsoft.com/library/en-us/sqldmo/dmoref_ob_s_7igk.asp

Also, here are some sample projects using sqldmo. These should be helpful.
http://www.gotdotnet.com/community/usersamples/Default.aspx?query=sqldmo
Unfortunately those didn't help much. One is just the Microsoft site that
doesn't explain anything (or is just plain wrong I don't know which). The
other only uses SQLDMO to get a list of servers. I've got the list, but it
doesn't do me any good unless I can connect to them.
Tom P.
 
H

Henry Padilla

oj said:
The Application object is just a shell. The connection/security info is
part of the Server object and you cannot access it unless you've
successfully connected to the server. This is by design else this would be
a major security hole!

Anyway, here is example using trusted authentication.
http://msdn.microsoft.com/library/en-us/sqldmo/dmoref_con03_8q44.asp

Thanks, I found a "Login" demo in the SQL Server directory and they try to
connect both ways and use whichever way works. Yep, there's an if()
statement that if connecting with secure login doesn't work try it with the
supplied user name and password.

Looks like that's my recourse.

Thanks for sticking with me through this. You were a big help.
Tom P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top