microsoft / testfx

MSTest framework and adapter
MIT License
716 stars 253 forks source link

How to get connect with DB in selenium MSTESTV2? #576

Closed shirishnagarnz closed 5 years ago

shirishnagarnz commented 5 years ago

Description

As a part of my TCs, I want to get connect to DB and execute queries. Tried to find out references but didn't find much. Is MSTESTV2 is supporting this? Is so, can someone please provide details to get connect DB while executing scripts in selenium?

For a defect specific to the MSTest V2 test framework, describe the issue you've observed.

acesiddhu commented 5 years ago

@shirishnagarnz mstest v2 doesn't restrict you from connecting to a DB. you can connect to DB in the same way as you will do in a normal console app. The connection of DB code will vary from DB engine to engine. Search for samples on how to connect to DB via c# and you should find something

parrainc commented 5 years ago

@shirishnagarnz as mentioned above, mstest v2 doesn't restrict you from doing that. Depending on the db you're planning to connect to it, you would have to use a specific library that help you doing that. There's a ton of example on the internet if you look for "how to connect to a sql database from c#".

Below is a short snippet that you could use right away to get connected to a SQL Server db:

var connectionString = "data source=YOUR_SQL_SERVER_NAME;initial catalog=YOUR_DB_NAME;persist security info=True;user id=USER_ID;password=PASS;MultipleActiveResultSets=True;";

using (SqlConnection con = new SqlConnection(this.ConnectionString))
{
   using (SqlCommand command = new SqlCommand(queryStatement, connection)
   {
        connection.Open();
        result = command.ExecuteReader();
        connection.Close();
   }
}

Of course, for the above code to work, you should have a SQL Server with a database on it. And the user/password part is optional, but you have to specify in the connectionString that you'd like to use WindowsAuth.

Hope it helps getting this resolved!

acesiddhu commented 5 years ago

@shirishnagarnz I am closing the issue. If you have any other queries do open another issue