Solved

Exists SQL Server

Posted on 2003-12-09
7
424 Views
Last Modified: 2010-07-27
my user can input some like this   SQLDatabaseServer.DataBaseName in a string variable
example
               dbvar :=  SQL-Server.Employee List  
first i need transform dbvar in a correct sql syntax   ----> dbvar := [SQL-Server].[Employee List]

now the big problem is how can i know if [SQL-Server] exists on my lan...

i need execute this
           
doSQL('select name from '+ dbvar+'.dbo.sysobjects where name='+quotedstr('MyTableName'))='MyTableName'

but i have errors like Could not find server 'SQL-Server' or Incorrect syntax near '-' if my dbvar still with SQL-Server

thank you.

0
Comment
Question by:ThoseBug
7 Comments
 
LVL 3

Expert Comment

by:Peter_
Comment Utility
Could be simply that SQL is a reserved word?

According to

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ra-rz_9oj7.asp

It is, at least in ODBC.

Try naming the server as something else, maybe that will do it?
0
 
LVL 6

Expert Comment

by:bpana
Comment Utility
hi,

first of all, you have to know all the connection parameters for the SQL Server.
if you know them, you can do:

function checkSQL(aServerName: string; aConnectionString: string): boolean;
var
  conn : TADOConnection;
begin
  Result := False;
  conn := TADOConnection.Create(nil);
  conn.ConnectionString := aConnectionString;
  conn.CommandTimeout := 2;
  try
    try
      conn.Open;
      Result := True;
    except
      ShowMessage('Cannot connect to server: ' + aServerName);
    end;
  finally
    if conn.Connected then
      conn.Close;
  end;
end;

a connectionstring should look like:
'Provider=SQLOLEDB.1;Persist Security Info=True;Data Source=***;User ID=***;Password=***'
where:
Data Source = SQL ServerName
User ID = SQL login
Password = SQL password

Bogdan
0
 

Expert Comment

by:roburobe
Comment Utility
To check if [SQL-Server] exists on your lan, you can check that if on that server, the SQL Server service is running.

You can use this line of code:
  if serviceRunning('\\'+SQLServer,'mssqlserver') then

putting these 3 little procedures into your source:

//-------------------------------------
// get service status
//
// return status code if successful
// -1 if not
//
// return codes:
//   SERVICE_STOPPED
//   SERVICE_RUNNING
//   SERVICE_PAUSED
//
// following return codes
// are used to indicate that
// the service is in the
// middle of getting to one
// of the above states:
//   SERVICE_START_PENDING
//   SERVICE_STOP_PENDING
//   SERVICE_CONTINUE_PENDING
//   SERVICE_PAUSE_PENDING
//
// sMachine:
//   machine name, ie: \\SERVER
//   empty = local machine
//
// sService
//   service name, ie: Alerter
//
function ServiceGetStatus(
  sMachine,
  sService : string ) : DWord;
var
  //
  // service control
  // manager handle
  schm,
  //
  // service handle
  schs   : SC_Handle;
  //
  // service status
  ss     : TServiceStatus;
  //
  // current service status
  dwStat : Longint;
begin
  dwStat := -1;

  // connect to the service
  // control manager
  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_CONNECT);

  // if successful...
  if(schm > 0)then
  begin
    // open a handle to
    // the specified service
    schs := OpenService(
      schm,
      PChar(sService),
      // we want to
      // query service status
      SERVICE_QUERY_STATUS);

    // if successful...
    if(schs > 0)then
    begin
      // retrieve the current status
      // of the specified service    
      if(QueryServiceStatus(
           schs,
           ss))then
      begin
        dwStat := ss.dwCurrentState;
      end;

      // close service handle
      CloseServiceHandle(schs);
    end;

    // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;

  Result := dwStat;
end;

//-------------------------------------
// return TRUE if the specified
// service is running, defined by
// the status code SERVICE_RUNNING.
// return FALSE if the service
// is in any other state, including
// any pending states
//
function ServiceRunning(
  sMachine,
  sService : string ) : boolean;
begin
  Result := SERVICE_RUNNING =
    ServiceGetStatus(
      sMachine, sService );
end;


//-------------------------------------
// return TRUE if the specified
// service was stopped, defined by
// the status code SERVICE_STOPPED.
//
function ServiceStopped(
  sMachine,
  sService : string ) : boolean;
begin
  Result := SERVICE_STOPPED =
    ServiceGetStatus(
      sMachine, sService );
end;
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Author Comment

by:ThoseBug
Comment Utility
thank you roburobe
i have error in
 schs   : SC_Handle;
and others variables with a lot of errors...
0
 
LVL 3

Expert Comment

by:Peter_
Comment Utility
If you are likely to have Enterprise manager (SQL Server tools) installed on the client machine, then you may activate any Enterprise Manager function or menu from within your appliction by using SQLNS and/or SQLDMO.

For a sample on how, see my answer to question below about making an SQL-Server backup:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20691981.html

You could use this function to look for a database as an example:

function GetDBNameSpaceObject(
  const aDBName: String): SQLNamespaceObject;
var
  hServer,hDatabases,hDatabase:Integer;
begin
   Result:=nil;
   hServer:=FNameSpace.GetRootItem;
   hDatabases:=FNameSpace.GetFirstChildItem(hServer,SQLNSOBJECTTYPE_DATABASES,'');
   hDatabase:=FNameSpace.GetFirstChildItem(hDatabases,SQLNSOBJECTTYPE_DATABASE,aDBName);
   Result:=FNameSpace.GetSQLNameSpaceObject(hDatabase);
end;

Once imported, these units (SQLNS, SQLDMO) give access to lots of powerful easy to use functions (like browsing all servers in your network).

I don't have sql-server installed where I'm at right now, so I cant provide a full sample for this exact problem. But if you have been using the tools before, then just browse the units and you figure it out. The only downside is that the sql server tools will have to be installed on the client machine or it wont work.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (75)

Computer101
E-E Admin
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now