Palak Shah
Well-known member
- Joined
- Apr 29, 2020
- Messages
- 97
- Programming Experience
- 1-3
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
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
Function Call:
In my excel - I have two sheets: LogInUserData and Sheet2
Now in LogInUserData
And in Sheet2
Key | Klarna |
LogInSuccessfully | Demo |
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
C#:
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:
C#:
LogInUserModel user = AccessExcelData.GetTestData<LogInUserModel>("LogInPageData.xlsx", "LogInUserData", "LogInSuccessfully");
string email = user.Email.ToString();
Last edited by a moderator: