Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
GeneralRe: Connecting to remote MySQL database: cannot open connection Pin
mwholt4-Nov-07 8:43
mwholt4-Nov-07 8:43 
AnswerRe: Connecting to remote MySQL database: cannot open connection Pin
Paul Conrad4-Nov-07 4:44
professionalPaul Conrad4-Nov-07 4:44 
QuestionHow to select the Server Pin
Assaf824-Nov-07 1:26
Assaf824-Nov-07 1:26 
AnswerRe: How to select the Server Pin
Koltz4-Nov-07 1:34
Koltz4-Nov-07 1:34 
GeneralRe: How to select the Server Pin
Assaf824-Nov-07 1:47
Assaf824-Nov-07 1:47 
AnswerRe: How to select the Server Pin
I Believe In GOD4-Nov-07 2:05
I Believe In GOD4-Nov-07 2:05 
GeneralRe: How to select the Server Pin
Assaf824-Nov-07 2:08
Assaf824-Nov-07 2:08 
GeneralRe: How to select the Server Pin
I Believe In GOD4-Nov-07 2:14
I Believe In GOD4-Nov-07 2:14 
using System;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
<br />
	public class SqlLocator<br />
	{<br />
		[DllImport("odbc32.dll")]<br />
		private static extern short SQLAllocHandle(short hType, IntPtr inputHandle, out IntPtr outputHandle);<br />
		[DllImport("odbc32.dll")]<br />
		private static extern short SQLSetEnvAttr(IntPtr henv, int attribute, IntPtr valuePtr, int strLength);<br />
		[DllImport("odbc32.dll")]<br />
		private static extern short SQLFreeHandle(short hType, IntPtr handle); <br />
		[DllImport("odbc32.dll",CharSet=CharSet.Ansi)]<br />
		private static extern short SQLBrowseConnect(IntPtr hconn, StringBuilder inString, <br />
			short inStringLength, StringBuilder outString, short outStringLength,<br />
			out short outLengthNeeded);<br />
<br />
		private const short SQL_HANDLE_ENV = 1;<br />
		private const short SQL_HANDLE_DBC = 2;<br />
		private const int SQL_ATTR_ODBC_VERSION = 200;<br />
		private const int SQL_OV_ODBC3 = 3;<br />
		private const short SQL_SUCCESS = 0;<br />
		<br />
		private const short SQL_NEED_DATA = 99;<br />
		private const short DEFAULT_RESULT_SIZE = 1024;<br />
		private const string SQL_DRIVER_STR = "DRIVER=SQL SERVER";<br />
	<br />
		//private SqlLocator(){}<br />
<br />
		public static string[] GetServers()<br />
		{<br />
			string[] retval = null;<br />
			string txt = string.Empty;<br />
			IntPtr henv = IntPtr.Zero;<br />
			IntPtr hconn = IntPtr.Zero;<br />
			StringBuilder inString = new StringBuilder(SQL_DRIVER_STR);<br />
			StringBuilder outString = new StringBuilder(DEFAULT_RESULT_SIZE);<br />
			short inStringLength = (short) inString.Length;<br />
			short lenNeeded = 0;<br />
<br />
			try<br />
			{<br />
				if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_ENV, henv, out henv))<br />
				{<br />
					if (SQL_SUCCESS == SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(IntPtr)SQL_OV_ODBC3,0))<br />
					{<br />
						if (SQL_SUCCESS == SQLAllocHandle(SQL_HANDLE_DBC, henv, out hconn))<br />
						{<br />
							if (SQL_NEED_DATA ==  SQLBrowseConnect(hconn, inString, inStringLength, outString, <br />
								DEFAULT_RESULT_SIZE, out lenNeeded))<br />
							{<br />
								if (DEFAULT_RESULT_SIZE < lenNeeded)<br />
								{<br />
									outString.Capacity = lenNeeded;<br />
									if (SQL_NEED_DATA != SQLBrowseConnect(hconn, inString, inStringLength, outString, <br />
										lenNeeded,out lenNeeded))<br />
									{<br />
										//throw new ApplicationException("Unable to aquire SQL Servers from ODBC driver.");<br />
									}	<br />
								}<br />
								txt = outString.ToString();<br />
								int start = txt.IndexOf("{") + 1;<br />
								int len = txt.IndexOf("}") - start;<br />
								if ((start > 0) && (len > 0))<br />
								{<br />
									txt = txt.Substring(start,len);<br />
								}<br />
								else<br />
								{<br />
									txt = string.Empty;<br />
								}<br />
							}						<br />
						}<br />
					}<br />
				}<br />
			}<br />
			catch //(Exception ex)<br />
			{<br />
				#if (DEBUG)<br />
				#endif <br />
				txt = string.Empty;<br />
			}<br />
			finally<br />
			{<br />
				if (hconn != IntPtr.Zero)<br />
				{<br />
					SQLFreeHandle(SQL_HANDLE_DBC,hconn);<br />
				}<br />
				if (henv != IntPtr.Zero)<br />
				{<br />
					SQLFreeHandle(SQL_HANDLE_ENV,hconn);<br />
				}<br />
			}<br />
	<br />
			if (txt.Length > 0)<br />
			{<br />
				retval = txt.Split(",".ToCharArray());<br />
			}<br />
<br />
			return retval;<br />
		}<br />
	}<br />


This Class return All the SQL Server , from the Other PC's on the network
by using this Function GetServers

(THIS CLASS RETURN ONLY THE PC'S That SQL SERVER INSTALLED ON IT)

P.S
it's return String Array Which is easily Can add to combo box

Kind regards ,

I know nothing , I know nothing

GeneralRe: How to select the Server Pin
Jimmanuel4-Nov-07 4:52
Jimmanuel4-Nov-07 4:52 
GeneralRe: How to select the Server Pin
Paul Conrad4-Nov-07 6:10
professionalPaul Conrad4-Nov-07 6:10 
GeneralRe: How to select the Server Pin
Koltz4-Nov-07 6:26
Koltz4-Nov-07 6:26 
GeneralRe: How to select the Server Pin
I Believe In GOD5-Nov-07 4:42
I Believe In GOD5-Nov-07 4:42 
GeneralRe: How to select the Server Pin
Jimmanuel5-Nov-07 4:56
Jimmanuel5-Nov-07 4:56 
GeneralRe: How to select the Server Pin
Assaf824-Nov-07 2:25
Assaf824-Nov-07 2:25 
AnswerRe: How to select the Server Pin
Ed.Poore4-Nov-07 2:58
Ed.Poore4-Nov-07 2:58 
QuestionCheck which line was clicked? Pin
Gadjuka4-Nov-07 1:03
Gadjuka4-Nov-07 1:03 
AnswerRe: Check which line was clicked? Pin
Colin Angus Mackay4-Nov-07 1:23
Colin Angus Mackay4-Nov-07 1:23 
AnswerRe: Check which line was clicked? Pin
Anthony Mushrow4-Nov-07 1:45
professionalAnthony Mushrow4-Nov-07 1:45 
GeneralRe: Check which line was clicked? Pin
Kristian Sixhøj4-Nov-07 1:49
Kristian Sixhøj4-Nov-07 1:49 
GeneralRe: Check which line was clicked? Pin
Anthony Mushrow4-Nov-07 3:04
professionalAnthony Mushrow4-Nov-07 3:04 
GeneralRe: Check which line was clicked? [modified] Pin
Kristian Sixhøj4-Nov-07 3:07
Kristian Sixhøj4-Nov-07 3:07 
GeneralRe: Check which line was clicked? Pin
Gadjuka4-Nov-07 4:30
Gadjuka4-Nov-07 4:30 
GeneralRe: Check which line was clicked? Pin
Kristian Sixhøj4-Nov-07 4:44
Kristian Sixhøj4-Nov-07 4:44 
GeneralRe: Check which line was clicked? Pin
Daniel Grunwald4-Nov-07 5:03
Daniel Grunwald4-Nov-07 5:03 
AnswerRe: Check which line was clicked? Pin
Tamimi - Code4-Nov-07 1:49
Tamimi - Code4-Nov-07 1:49 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.