简体   繁体   中英

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.

No need for batch files if you just want to check if you can connect (ie 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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM