Click here to Skip to main content
Click here to Skip to main content

Finding SQL Servers on the Network

By , 22 Dec 2003
 

Sample Image - DBGrep.jpg

Introduction

I am a TSQL fanatic. The programs I code are highly dependent upon MS SQL stored procedures. I pay for this love of TSQL when a major overhaul of the system is necessary. Sometimes my code needs to be updated heavily in two places; client and server. To facilitate these updates, I created a database searching program I call DB Grep. It uses Regex to search out every reference to words or phrases in an entire database. The program has saved many hours of research and gives me the warm fuzzy "I didn’t miss anything" feeling.

When developing DB Grep, I ran into an interesting problem. How do I find the SQL Servers on my network?

Options

After many hours of web research, I came up with the following alternatives:

Option Pro Con
No Location Services. No code to write. Typing in SQL server names by memory is a pain.
Use Windows OS Services. Using NetServerEnum is very fast. Does not always return the desired results. It returns Windows server names, not SQL Server names, and they are not always the same. It also does not work well on a non-domain based network. Couldn't find my local MSDE server.
Use the SQLDMO objects. Returns the desired results. Installation headaches. Installing COM objects is always problematic, not to mention possible license issues.
Use ODBC Returns the desired results. Should already be installed. Haven't found any yet.

Needless to say, I picked the ODBC solution. This required a bit of research with a lot of PInvoke trial and error.

I should state up front that this has not been tested on Windows 95/98/ME. I have decided that these operating systems are no longer necessary for my new development. The code has been tested on Windows 2000 and XP using Framework 1.1.

The Process

In order to acquire the names of the available SQL servers from ODBC, we have to allocate an environment, set the ODBC style and connect to the ODBC service. SQLAllocHandle() is used to get the environment and connection handles. In between the calls, it is necessary to specify what version of ODBC is to be used, by calling SQLSetEnvAttr(). I choose ODBC 3.0 using a system constant. Of course, you must always play nice with the ODBC resources by releasing both the environment and connection handles with matching calls to SQLFreeHandle().

I wrapped the allocation calls in a try block and the free calls in the finally section to ensure the release of the ODBC resources. The necessary PInvoke declarations to setup and tear down the ODBC environment are as follows:

private const short SQL_HANDLE_ENV = 1;
private const short SQL_HANDLE_DBC = 2;
private const int SQL_ATTR_ODBC_VERSION = 200;
private const int SQL_OV_ODBC3 = 3;
private const short SQL_SUCCESS = 0;


[DllImport("odbc32.dll")]
private static extern short SQLAllocHandle(
    short hType, 
    IntPtr inputHandle, 
    out IntPtr outputHandle);
[DllImport("odbc32.dll")]
private static extern short SQLSetEnvAttr(
    IntPtr henv, 
    int attribute, 
    IntPtr valuePtr, 
    int strLength);
[DllImport("odbc32.dll")]
private static extern short SQLFreeHandle(
    short hType, 
    IntPtr handle);

Once the connection has been established, we can use a trick of the SQL ODBC driver to locate the advertising MS SQL servers. I attempt to open a MS SQL database connection using SQLBrowseConnect() by specifying only the SQL driver. The driver accommodates my request by building a connection string with the required parameters filled in with the possible values it can derive (like the available MS SQL Servers). It then returns a value stating that it needs more information. The server names can be easily parsed out of this connection string.

For speed, I pre-allocate a StringBuilder with a capacity of 1024 characters. In case of an extra large list of available servers, I test an out parameter to see if a larger string is necessary. I recall the SQLBrowseConnect() function with the newly resized string if necessary. The PInvoke for SQLBrowseConnect() call follows:

private const short SQL_NEED_DATA = 99;
private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";
 
[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]
private static extern short SQLBrowseConnect(
    IntPtr hconn, 
    StringBuilder inString, 
    short inStringLength, 
    StringBuilder outString, 
    short outStringLength, 
    out short outLengthNeeded);

For example, I pass "DRIVER=SQL SERVER" into SQLBrowseConnect() and get something like "SERVER:Server={(local),SQL_SERVER1,SQL_BKSVR};UID:Login ID=?; PWD:Password=?; *APP:AppName=?; *WSID:WorkStation ID=?" returned. It is a simplistic task to pull out the comma delimited substring between the two curly braces. To simplify the use of the server list, I call the Split() method on the substring to return a string array that can be used in a foreach statement.

For reuse, I encapsulated the PInvoke declarations and the static method within a class. Since this is just a helper method, I took precautions to hide any failures from release code and return a null string[] value in that case. A null return value indicates that no servers were found. Here is an example of calling the resultant code:

string[] theAvailableSqlServers = SqlLocator.GetServers();
if (theAvailableSqlServers != null)
{
    myListBox.DataSource = theAvailableSqlServers;
} 
else
{
    MessageBox.Show("No SQL servers found.");
}

Summary

This code is more research than skill derived. Hopefully, I am able to save you a few hours of trudging through MSDN on a future project. If you do a lot of MS SQL work, you may be interested in my DB Grep program, which is freely available (with source) on GotDotNet. It currently works very well in my environment but, I am sure it could use a good workout elsewhere.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Michael Potter
Chief Technology Officer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralLink for DB Grep ToolmemberMember 142514920 Jan '11 - 1:51 
Hi
 
Thanks for your code, it is very useful for me.
 
given link is broken
 
Could you please tell me new link for Grep Source Code?
 
Thanks,
Karl
QuestionCan you send me the form related to this program?memberlathavarma29 Sep '10 - 22:24 
Your code suits my need, but not able to use as the form is not attached. Can you please provide the form?
GeneralSuggested improvement using Regexmemberkjhduyudmhmxc31 Jul '10 - 17:42 
I'm not sure if the string returned from SQLBrowseConnect will always be the same, but I'm going to use regex to locate the server names.
 
Regex regex = new Regex("Server={(.+)}", RegexOptions.IgnoreCase);
Match match = regex.Match(outString.ToString());
 
if (match.Groups.Count != 2)
throw new ApplicationException("Cannot locate formatted SQL Server names.");
 
text = match.Groups[1].Value;
 
which will replace
 
txt = outString.ToString();
int start = txt.IndexOf("{") + 1;
int len = txt.IndexOf("}") - start;
 
if ((start > 0) && (len > 0))
txt = txt.Substring(start, len);
else
txt = string.Empty;
GeneralLink for DB GrepmemberMember 39203771 Oct '09 - 17:34 
link broken Frown | :(
 
where can get the DB Grep Source Code?
QuestionLicensememberSparkley Doodads14 Jul '08 - 7:37 
Hey, Great class!
 
I was curious as to what license you've released this under. If you could let me know, that'd be great!
 
Thanks!
AnswerRe: LicensememberMichael Potter14 Jul '08 - 7:59 
This is very old code written when .Net 1.0 was king. I believe there are better ways of doing this now.
 
That said: This code (and any other code I have released on this site) can be used anyway you want. I consider it all public domain.
GeneralVery BadmemberDevelopmentTech6 Jul '08 - 4:49 
Very Bad
GeneralThanksmemberGeethanga Amarasinghe9 Oct '07 - 18:19 
Thanks buddy,
this is cool, keep up the good work.Smile | :)
Questionconnect to database in sql?memberlildiapaz5 Jul '07 - 4:11 
Hi, I'm a newbie to c# programming. Can someone please explain to me how to connect to a database from another pc when the server name is provided by the user in a text box. What I want to happen is the user provides the server name, and then I want to connect to a database by using the appropriate login Also is there a way to require the user to provide a user name and password using sql authentication.
 
here's what I tried but it didn't seem to work
 
SqlConnection myConnection =new SqlConnection();
System.Data.SqlClient.SqlConnection conn=new System.Data.SqlClient.SqlConnection();
myConnection.ConnectionString="Persist Security Info=False; Server=myServer; Data Source=remote_machine; UID=*****; PWD=*****; Database=your-database";
 

Any help would be greatly appreciatedConfused | :confused:
 
try
{
myConnection.Open()
textbox1.Text="Connected!";
}
catch(Exception ex)
{
MessageBox.Show("Failed");
}
finally
{
myConnection.Close();
}
Generalusing your GetSqlClassmemberRepliCrux19 Jun '07 - 15:12 
Hi Michael,
 
I am using your GetSqlClass to browse servers in my project. If complete I will give your referecne in the class before I display in code project.
 
Thanks
GeneralUse with Sybase 9memberChris_McGrath6 Apr '06 - 15:06 
I need a class similar to this only it looks up Sybase 9 databases. I tried changing the SQL_DRIVER_STR to "DRIVER=Adaptive Server Anywhere 9.0" but it still doesn't work. Any ideas?
 
Cheers,
Chris
QuestionCannot see local sql server (MSDE) when .NET Framework 2.0 is installedmemberCaJuDo23 Jan '06 - 0:03 
When i install the .net framework 1.1 and .net framework 2.0 I cannot see my local msde db.
When i deinstall framework 1.1 everything works fine.
What can I do? Is it possible to have both frameworks installed and use SQLBrowseConnect?
 
Thanks a lot!
 

AnswerRe: Cannot see local sql server (MSDE) when .NET Framework 2.0 is installedmemberTommi G25 Oct '06 - 15:13 
.NET 2.0 supports direct finding of SQL Server instances by a class under System.Data (which name I just can't remember).
GeneralApp Failed to Initializememberdna199012 Jan '06 - 6:28 
Running DBGrep.exe, I get the above message.
 
What did I miss? Is there a .NET, COM, etc type of install needed?
GeneralRe: App Failed to InitializememberMichael Potter12 Jan '06 - 8:04 
No idea - I think I wrote in Framework 1.1 (that is all it should require). It has been awhile. You are welcome to the source if you would like it.
GeneralHelp me,please.memberSinbaQL29 Dec '05 - 17:07 
I can't understand SQLBrowseConnect() very clearly.
For example, If I pass "Driver=SQL Server;SERVER=OTHER;UID=; PWD=" or "Driver=SQL Server" into SQLBrowseConncetion() will get the same result.
Of course, I can't use uid named null to connect the server named OTHER, but how can I get the error information.D'Oh! | :doh:
Thanks.
 
-- modified at 23:08 Thursday 29th December, 2005
QuestionHow get the Infomation about the stored procedures and so on.memberSinbaQL29 Dec '05 - 14:45 
I don't know how to search these objects. Such as search some parameters in the stored procedures belong to the selected sever and database.
Thanks.Smile | :)

GeneralNew link for DB GrepmemberEnrico Detoma26 Dec '05 - 23:29 
Hi,
 
the link for DB Grep on GotDotNet is broken.
Here is the new link: http://www.gotdotnet.com/workspaces/workspace.aspx?id=f83af6a8-fa84-4af8-bd8f-f2daa568c7a6[^]
 
Thank you for sharing! Smile | :)
 
Enrico

GeneralRe: New link for DB GrepmemberSinbaQL27 Dec '05 - 22:05 
Big Grin | :-D
Thanks. I love all of you.
GeneralNew Class supportmemberwschlichtman10 Sep '05 - 14:00 
As of .NET 2.0, you can do the same thing with the following:
 
using System.Data.Sql;
using System.Windows.Forms;
 
private bool GetServers()
{
SqlDataSourceEnumerator sqlDse = SqlDataSourceEnumerator.Instance;
DataTable dt = sqlDse.GetDataSources();
 
foreach (System.Data.DataRow row in dt.Rows)
{
listBox1.Items.Add((string)row["ServerName"]);
}
return (listBox1.Items.Count > 0);
}
 

GeneralNice work!memberkris.mackintosh14 Jul '05 - 12:43 
I dont supose you would consider adding the source to the connection string editor you have in your application, im currently using the standard "data link" but i'd like to customize it more to what you have
thanks in advance
 
-Kris
Questionmanipulating server?memberUnruled Boy19 Jun '05 - 17:41 
start/pause/stop the ms sql serverD'Oh! | :doh:
 
Regards,
unruledboy@hotmail.com
QuestionI need to help: how to get sever name, database name to update connection string?memberheovissan2 Jun '05 - 17:23 
Hi all,
I'm doing a minor project. A part of it is: I intend to write a form which can load all the current sql server name and also the database name equivalent to each server. The user can choose it from combobox and test connection whether success or not. The server name and database name will affect to my connection string which used to access that database. Could you solved this problem? Thank you!
AnswerRe: I need to help: how to get sever name, database name to update connection string?memberSinbaQL28 Dec '05 - 2:54 
I can't understant very clearly.
connection string??
Do you mean the SQLBrowseConnectConfused | :confused:
GeneralProblem ...please helpmemberpubududilena8 May '05 - 16:56 
hi all,
I have used above theory to find SQL Server and databases in My C# Application. I have check it in our Computers which are in NETwork.
In one computer ,I have taken this serious probelm.

I have taken SQL Server Names in the Database List BOX..SQL Server List also showing the SQL Server List Box.I didn't change any code part there.
..
 
Can u please tell me..what's wrong in there??

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 23 Dec 2003
Article Copyright 2003 by Michael Potter
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid