|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI 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? OptionsAfter many hours of web research, I came up with the following alternatives:
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 ProcessIn 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. I wrapped the allocation calls in a 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 For speed, I pre-allocate a 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 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 string[] theAvailableSqlServers = SqlLocator.GetServers();
if (theAvailableSqlServers != null)
{
myListBox.DataSource = theAvailableSqlServers;
}
else
{
MessageBox.Show("No SQL servers found.");
}
SummaryThis 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.
|
|||||||||||||||||||||||||||||||||||||