2

I'm running into an issue when I try to run a simple Selenium script which has field (SSN), and on every new run of the script SSN need to has bigger value (+1). Is it possible Selenium to connect with SQL Server database and check the SSN value from there, and what is the code to connect Selenium with SQL Server database? I am using Selenium with C#. Many Thanks.

1
  • 1
    Selenium automates browsers. It do not connect with Database, C# will do that part. Commented Aug 13, 2015 at 12:40

1 Answer 1

2
public static int SSN_DB(IWebDriver session, string connString) {
  //creating a db connection connection
  var DBConnection = new SqlConnection(connString);
  DBConnection.Open();
  // Can execute a query or store procedure 
  var MemTable = new SqlCommand();
  MemTable.CommandText = "Select top 1 SSN from dbo.member order by EMPL desc";
  MemTable.Connection = DBConnection;
  var dr = MemTable.ExecuteReader();
  var Memberid = 0;

  while (dr.Read()) {
    Memberid = dr.GetInt32(0);
    Console.WriteLine(Memberid);
  }
  return Memberid;
}

}
Sign up to request clarification or add additional context in comments.

1 Comment

SqlConnection have to used in scope of using, otherwise you will endup with memory leak!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.