Check whether SQL statements are installed on the computer on the network.

Source: Internet
Author: User
Reference the SQL DMO component
// Obtain the names of all available SQL servers in the local area network.
Cmbserver. Items. Clear ();
Try
{
Sqldmo. Application app =   New Sqldmo. applicationclass ();
Sqldmo. namelist list = App. listavailablesqlservers ();
Int Icount = List. count;

For ( Int I =   0 ; I < Icount; I ++ )
{
String Stemp = List. item (I );
If (Stemp ! =   Null )
Cmbserver. Items. Add (stemp );
}
}
Catch
{< br> // if an error occurs when obtaining the sqldmo component, by default, the local name is written in
MessageBox. show ( " the server list cannot be obtained, sdldmo may be missing. DLL! " );
cmbserver. items. add (system. net. DNS. gethostname ();
}
Why does the example I used panyee (Happy Prince) always show "unable to obtain the Server LIST, maybe it is missing sdldmo. dll"? I have this file!

If "http: // Xml.sz.luohuedu.net/xml/showdetail.asp? The example in ID = BCEAADFB-CFF3-4804-B3B3-6C7D6488982B does not show the following information:
" The error message returned when the unprocessed system.invalidcastexceptionexceptionexception exists in windowsapplication1.exe.
Other information: the QueryInterface of interface sqldmo. namelist fails.
What's going on? Could you help me!
First, your SQL Server version is insufficient.
To use sqldmo. dll, download SQL SP2.
Second, if you want to list all SQL servers in the LAN
We recommend that you use the isql.exe file that comes with SQL Server as long as it is an SQL Server File. 6 . 5 or above.
The following is the source code:
String Filename =   " C: \ Program Files \ Microsoft SQL Server \ 80 \ tools \ binn \ isql.exe " ;
If (System. Io. file. exists (filename ))
{
System. Diagnostics. processstartinfo =   New System. Diagnostics. processstartinfo (filename, " -L " );
Processstartinfo. useshellexecute =   False ;
Processstartinfo. createnowindow =   True ;
Processstartinfo. redirectstandardoutput =   True ;
Processstartinfo. redirectstandarderror =   True ;
System. Diagnostics. Process = System. Diagnostics. process. Start (processstartinfo );
Process. waitforexit ();
Cboserverlist. Items. Clear ();
Int Line =   1 ;
String Server =   Null ;
While (Process. standardoutput. Peek () >   - 1 )
{
Server = Process. standardoutput. Readline (). Trim ();
Line + = 1 ;
If (Line >   6 )
{
Cboserverlist. Items. Add (server );
}
Server =   Null ;
}
}
Cboserverlist. Items. Remove (system. environment. machinename );
Cboserverlist. Items. Add ( " Localhost " );
Cboserverlist is a comobox

You can enter isql.exe in "example ". - ? Check whether the parameter sequence is what you want.
To list SQL servers in a LAN, enter iSQL. - L.
Private   Void Cmbdatabase_enter ( Object Sender, system. eventargs E)
{
// Obtain the names of tables on a server.

String Strserver = Cmbserver. text;
String Struid = Txtuid. text;
If (Strserver. Trim () ! =   ""   && Struid. Trim () ! =   "" )
{
String Strpwd = Txtpwd. text;
String Strconn =   " Server = "   + Strserver +   " ; Database = Master; uid = "   + Struid +   " ; Pwd = "   + Strpwd;
Sqlconnection Conn =   Null ;
Try
{
Conn =   New Sqlconnection (strconn );
String Strsql =   " Select * From sysdatabases order by dbid " ;
Sqldataadapter cmd =   New Sqldataadapter (strsql, Conn );
Dataset DS =   New Dataset ();
Cmd. Fill (DS, " Databases " );
Cmbdatabase. Items. Clear ();
For ( Int I =   0 ; I < DS. Tables [ " Databases " ]. Rows. Count; I ++ )
{
String Strdb = DS. Tables [ " Databases " ]. Rows [I] [ " Name " ]. Tostring ();
Cmbdatabase. Items. Add (strdb );
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. tostring ());
}
Finally
{
If(Conn. State=Connectionstate. open)
Conn. Close ();
}
}
This . Cursor = Cursors. default;
}

From: http://www.cnblogs.com/ghd258/archive/2006/05/16/401907.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.