I need to test a database connection from my wpf application, that is, a method like:
bool TestCon(string dbserver, string user, string password, string dbname)
{
bool success;
// code
return success;
}
I have seen I can use sqlcmd -S dbserver -U user -P password -d dbname, but it's not clear how can I read an answer from sqlcmd so my program can say: connection successful!! or bad login!! or bad server!! Thanks.
Do you have to use sqlcmd? If not, you would be much better off to use ADO.NET...
bool TestCon(...)
{
try
{
using(var connection = new SqlConnection("your connection string"))
{
connection.Open();
connection.Close();
return true;
}
}
catch
{
return false;
}
}