|
Hi , Have a good Day
if SQL Server Installed on the same PC With your Application
(Not a network connection)
Simply :
Type Your Server Name "(local)" With out the Quotation , But included the Arc's
(local) (local) (local)
----
if you are planing to use your Application on the Other NetWork Pc's
please let me know , I have a Super class For Doing This
Kind regards
I know nothing , I know nothing
|
|
|
|
|
yes i plan to use my app. on other Pc
Thanks in advance Stark
Assaf
|
|
|
|
|
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 />
<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 />
} <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
{<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
|
|
|
|
|
Ever heard of SMO? All of that gets replaced with 5 lines of code:
DataTable dtServers = SmoApplication.EnumAvailableSqlServers(false);
foreach (DataRow row in dtServers.Rows)
{
string sqlServerName = row["Server"].ToString();
if (row["Instance"] != null && row["Instance"].ToString().Length > 0)
sqlServerName += @"\" + row["Instance"].ToString();
}
SMO Essentials[^]
MSDN[^]
|
|
|
|
|
I like that approach better
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Realy nice solution, thanks!
|
|
|
|
|
yeah I hear of it , thank for you
but I am still using VS 2003 . NET 1.1 Which didn't support the SMO
thank you so much for this information
kind regards
I know nothing , I know nothing
|
|
|
|
|
you could try SQL DMO instead - it's the predecessor to SMO which has most of the same functionality, in this case:
static public string[] GetSQLServers()
{
List<string> retval = new List<string>();
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList serverList = (SQLDMO.NameList)sqlApp.ListAvailableSQLServers();
foreach (string server in serverList)
{
retval.Add(server);
}
return retval.ToArray();
}
|
|
|
|
|
I try the value (local) but it not working it give me an error:
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
but when i put the name of the server: Toshiba-owner\Dev
everything go well.
Thanks
Assaf
|
|
|
|
|
|
My question seems simple, but I really can't find the answer!
How can I check which line in a TextBox the user has clicked on with the mouse??
|
|
|
|
|
It has been a while, but I vaguely remember it is with Selection. When you click the cursor is moved to where you click and the current selection is updated (even if the selection length is zero, i.e. nothing is selected). From the position of the selection you can work out which line has been clicked on.
|
|
|
|
|
Or you could just get the cursor position after the user clicks.
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
Wouldn't the form's top-left corner have to be at 0,0 at all times for that to work properly? I mean, if the user moves the form around, the cursor position will vary every time he/she clicks.
Virtual1ty "Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
No, no, no, no no. I meant the regular cursor -> |
Not the mouse cursor . There should be a property for the text box, i think its either textBox.Cursor, textBox.Cursor.Position, or textBox.CursorPosition. Or something like that anyway.
Of course, that would give you the index of the current letter the cursor is next to, it would then be up to you to figure out what line that was, perhaps by looking for all newline symbols that occure before it (Environment.NewLine, or "\r\n")
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
Oh, I didn't know that
Edit:
The Undefeated: You're looking for textBox1.Cursor.HotSpot.ToString();
or textBox1.Cursor.HotSpot.X and textBox1.Cursor.HotSpot.Y
-- modified at 9:14 Sunday 4th November, 2007
Virtual1ty "Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
Hmm, TextBox.Cursor seems to be what kind of mouse cursor is displayed when over the TextBox.
I think they call the "|" a caret...but there is only one member that is called something like that - ScrollToCaret().
|
|
|
|
|
Yes, TextBox.Cursor can also return what cursor is used for the textbox. But it can also
return the cursor's position on the textbox:
textBox1.Cursor.HotSpot.X;
textBox1.Cursor.HotSpot.Y;
textBox1.Cursor.HotSpot.ToString();
Virtual1ty "Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"
|
|
|
|
|
That's not the cursor position, that's the hotspot position.
If you want the cursor position, textBox1.PointToClient(Control.MousePosition).
If you want the caret position, use textBox1.SelectionStart (when nothing is selected, this property returns the caret position).
|
|
|
|
|
you should also consider the WordWrap property
in your solution..
why you want to do this ??
When you get mad...THINK twice that the only advice
Tamimi - Code
|
|
|
|
|
|
The function below should read an xml file and parse the elements and content, and I'm updating it to read the element content and the attribute values. So far the problem is with reading element content.
When the code uses method System.Xml.XmlReader.ReadElementContentAsString(), the code raises an exception (see Error).
However when the code using the above method is commented out the code is reading xml attributes.
On another note, any guidelines (for beginners) for validating XML and creating an XSD schema for validating it? My understanding is that this is the way suggested by w3c.
Less specifically, there are other ways of validating XML, including XmlSchemaValidator class, which would be most recommended?
.NET Framework 2.0
Error
------
System.Xml.XmlException: 'Element' is an invalid XmlNodeType. //INVALID?!
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
at TestConsole1.Program.readXMLFile(String _fileName) in C:\Documents and Set
tings\user\My Documents\Visual Studio 2005\Projects\TestConsole1\TestConsole1\Pr
ogram.cs:line 79
WhiteSpace:
Element:
Text:
Element:
Text:
Element:
Text:
WhiteSpace:
System.Xml.XmlException: 'Element' is an invalid XmlNodeType.
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
at TestConsole1.Program.readXMLFile(String _fileName) in C:\Documents and Set
tings\user\My Documents\Visual Studio 2005\Projects\TestConsole1\TestConsole1\Pr
ogram.cs:line 79
WhiteSpace:
WhiteSpace:
Element:
Text:
WhiteSpace:
WhiteSpace:
Press any key to continue . . .
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace TestConsole1
{
class Program
{
static void Main(string[] args)
{
readXMLFile("sample.xml");
}
static private void readXMLFile(string _fileName)
{
int ws = 0;
int pi = 0;
int dc = 0;
int cc = 0;
int ac = 0;
int et = 0;
int el = 0;
int xd = 0;
int tx = 0;
XmlTextReader textReader = new XmlTextReader(_fileName);
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
if (nType == XmlNodeType.XmlDeclaration)
{
Console.WriteLine("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
if (nType == XmlNodeType.Comment)
{
Console.WriteLine("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
if (nType == XmlNodeType.Attribute)
{
Console.WriteLine("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
if (nType == XmlNodeType.Element)
{
try
{
Console.WriteLine("Element:" + textReader.Name.ToString());
el = el + 1;
}
catch (XmlException ex)
{
Console.WriteLine(ex.GetBaseException());
Console.WriteLine(ex.InnerException);
}
}
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
et = et + 1;
}
if (nType == XmlNodeType.Entity)
{
pi = pi + 1;
}
if (nType == XmlNodeType.DocumentType)
{
Console.WriteLine("Document:" + textReader.Name.ToString());
dc = dc + 1;
}
if (nType == XmlNodeType.Whitespace)
{
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
if (nType == XmlNodeType.Text)
{
Console.WriteLine("Text:" + textReader.Name.ToString());
tx = tx + 1;
}
}
}
}
}
Jon
|
|
|
|
|
Does the node which causes the error contain child elements? The MSDN documentation for the XmlReader.ReadElementContentString method states:
This method reads the start tag, the contents of the element, and moves the reader past the end element tag. It expands entities and ignores processing instructions and comments. The element can only contain simple content. That is, it cannot have child elements.
It also states that an XmlException exception will be raised when calling this method if the current element contains child elements.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
hello
I want to catch any value in the datareader begin with a special string
Sysdiagrams, if i the value begin with cath it'll be rejected
any idea will greatly appreciated
Thanks in advance
Assaf
|
|
|
|
|
you may use the string as an array ...
like arrayName[indexValue]...
and 2nd option is use a char pointer..and again you can manipulate is
as an array
rao
|
|
|
|