1

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.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
ramnz
  • 631
  • 1
  • 6
  • 24

2 Answers2

6

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;
    }
}
DotNetMatt
  • 648
  • 2
  • 9
  • 26
Paul C
  • 4,687
  • 5
  • 39
  • 55
0

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.

Community
  • 1
  • 1
donstack
  • 2,557
  • 3
  • 29
  • 44
  • 1
    Sounds like a lot of work for just testing a connection in C# – Paul C Jan 21 '14 at 16:44
  • 1
    if it is only matter of testing connection then you should go for what @CodeBlend is saying otherwise instead of creating file you can use process.StandardOutput.ReadToEnd(); – donstack Jan 21 '14 at 16:49