ramnz
ramnz

Reputation: 631

how to ping database from winform c#

I need to find out if my database is running, and I have to do it in a WinForm in C#.

From the command prompt (in windows 7) I run the following command:

dbping -c "uid=sos;pwd=cdpbszqe;eng=mydatabase;dbn=mydatabase;links=tcpip" -pd file -d > result.txt

The result is written (redirected) to the result.txt file
which I open and see if it was successful

How can i do the same from in C# code using WinForms? Is it possible to ping and get a result if successful? Do I have to ping and create a text file from the ping result, then open it in my C# App? What's the best way to proceed?

thanks.

Upvotes: 1

Views: 1613

Answers (2)

Paul C
Paul C

Reputation: 4776

No need for batch files if you just want to check if you can connect (i.e. it is running). We can test in a few line in C#.

private bool TestConnection()
{
    using (var conn = new SqlConnection("connection string")
    {
        try
        {
            conn.Open();
            return true;
        }
        catch(Exception ex)
        {
            //log exception
        }

        return false;
    }
}

Upvotes: 6

donstack
donstack

Reputation: 2715

just create a batch file with above command and run it from your windows forms application using Process class. Get some help from : Executing Batch File in C#

Which will create your result.txt file and you can read it using any File.ReadAllLines() or whatever method you want to use.

Upvotes: 0

Related Questions