#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    3
    Rep Power
    0

    How to check using C# if firebird is running and possibly start it?


    LS,

    I noticed a few times that that the firebird service may be down. My application will than ofcourse crash.

    Ofcourse using VS 2005 C# I can use a TRY CATCH and if the database files exists than it remains the obvious that firebird must be down. But some kind of Firebird check is more elegant and possibly there is a way to start up the firebird service.

    PS I'm using firebird version 2.00

    Kind regards,


    Clemens Linders
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    3
    Rep Power
    0

    The answer


    On a different forum Carlos gave me a answer.

    For other people that experience the same problem I would like to explain it a bit further.

    In order to find out if there is any problem reading the database (is done during the filling of the dataset) I now use:

    try
    {
    TheDataAdapter.Fill(TheDataSet);
    }
    catch
    {
    if (File.Exists(TheDataAdapter.SelectCommand.Connection.Database))
    {
    try
    {
    ServiceController FireBirdService = new ServiceController("FirebirdGuardianDefaultInstance");
    FireBirdService.Start();
    DeDataAdapter.Fill(TheDataSet);
    }
    catch
    {
    MessageBox.Show(ADataBaseOpenIngErrorProbablyServer);
    }
    }
    else
    {
    MessageBox.Show(ADataBaseHasBeenRemovedError);
    }
    }

    This works perfectly. I know some people disagree on checking the existens of the file as the user doesn't need to know what file actually is de database file. But regardless of that any stupid client is capable of removing just about any file. And when the customer calls and says I get a DataBase has been deleted error, than you know immediately what has happened.

    As is also mentioned in the dotnetfirebird.org, the starting up of the service will probably not work if Firebird is located on a server.

    One last thing I need to mention for novice users. When you search c# help for ServiceController you will see
    it's namespace is System.ServiceProcess. So you might expect that using System.ServiceProcess will enable you to use ServiceController (or directly System.ServiceProcess.ServiceController) but unfortunately that is not the case. You have to drop a ServiceController from the Toolbox on your screen.

    I hope this is usefull for any future readers.


    Kind regards,



    Clemens Linders

    Comments on this post

    • pabloj agrees : Thanks for posting the solution
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Post Here's my solution


    Since FireBird did not implement the ConnectionTimeout on it's .NET provider's FbConnection, and sometimes 30 seconds (the default I think) is a little too long to wait, I devised this Method to let me know if a FireBird Server is responding.


    Code:
    using System.Net;
    using System.Net.Sockets;
    using System.Configuration;
    
    private bool TestPort(string address, int port)
    {
        int timeout = 500;
        //store timeout in web.config
        if(ConfigurationManager.AppSettings["RemoteTestTimeout"] != null)
            timeout = int.Parse(ConfigurationManager.AppSettings["RemoteTestTimeout"]);
        var result = false;
        try
        {
            using(var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                IAsyncResult asyncResult = socket.BeginConnect(address, port, null, null);
                result = asyncResult.AsyncWaitHandle.WaitOne(timeout, true);
                socket.Close();
            }
            return result;
        }
        catch { return false; }
    }
    What this does is create a socket that will try and open a tcp port to the FireBird server. If it can't connect, false will be returned from the WaitOne call after the timeout period expires.

    Just use

    Code:
    var fbServerNameOrIP = "someserverName";
    var portNumber = 3050; //the default fb port
    if(!TestPort(fbServerNameOrIP, portNumber))
    {
        //do something when it fails
    }
    Just make sure your timeout is realistic for your network or you will receive false negatives. I use 500 milliseconds and that seems to work ok.

    Hope this helps someone.

IMN logo majestic logo threadwatch logo seochat tools logo