Currently, I am fetching data from particular sheet where it matched a "Key" value - But now I want to access data from all sheets where it matches "Key" value
In my excel - I have two sheets: LogInUserData and Sheet2
Now in LogInUserData
And in Sheet2
Key |
Klarna |
LogInSuccessfully |
Demo |
And in Sheet2
KeyKlarna
LogInSuccessfullyDemo
Now, I want to access values of Key "Loginsuccessfully" from both the sheets - I have code but that fetches it from only first sheet and not from other - so how do i access complex data sheet from excel
I'm using ODBC Connection
- public static string TestDataFileConnection(string fileName)
- {
- string Filename = "C:\\Users\\PalakS\\source\\repos\\Daily update\\UnitTestProject2 - 10-04\\UnitTestProject2\\ExcelData\\LogInPageData.xlsx";
- string connectionString = string.Format("Dsn=Excel Files;READONLY=false;DBQ={0};", Filename);
- System.Data.Odbc.OdbcCommand odbcCmd = new System.Data.Odbc.OdbcCommand("", new System.Data.Odbc.OdbcConnection(connectionString));
- return connectionString;
- }
- public static T GetTestData<T>(string fileName, string sheet, string keyName)
- {
- using (var connection = new OdbcConnection(TestDataFileConnection(fileName)))
- {
- connection.Open();
- var query = string.Format("select * from [{0}$]where key = '{1}'", sheet, keyName);
- var value = connection.Query<T>(query).FirstOrDefault();
- connection.Close();
- return value;
- }
- }
Function call:
- LogInUserModel user = AccessExcelData.GetTestData<LogInUserModel>("LogInPageData.xlsx", "LogInUserData", "LogInSuccessfully");
- string email = user.Email.ToString();