Reputation: 45
For simplicity, I removed some Methods in Accounts Class, some lines in Display Method and displayed only required.
class Accounts
{
public int AccountNo { get; set; }
public string Firstname { get; set; }
public int Balance { get; set; }
}
public void Dispaly()
{
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
var res = conn.Query<Accounts>("select * from Accounts").ToList();
foreach (Accounts ac in res)
{
Console.WriteLine("Account No : " + ac.AccountNo);
Console.WriteLine("Name : " + ac.Firstname);
Console.WriteLine("Balance : " + ac.Balance);
}
conn.Close();
return;
}
}
When I Call Display Method, DAPPER is Not Mapping Balance Value Correctly from DataBase.
/********************* MY OUTPUT *******************/
Account No : 12345
Name : SHINDE
Balance : 0
NOTE: If I rename Balance Method in Accounts Class to Amount. It was working fine. Using DAPPER, There is any relation between Method Name in my Program and Column Name in DB?
Upvotes: 0
Views: 1321
Reputation: 1062520
With the edit, we can see that the table doesn't have a Balance
- it has Amount
. You will need to give it some help. You could rename the Balance
property to Amount
in your C# code, or you could use the query to tweak it, for example:
var res = conn.Query<Accounts>(
"select AccountNo, FirstName, Amount as Balance from Accounts").AsList();
(note that AsList
here is slightly more efficient than ToList
- it avoids an extra few allocations; also note that you don't actually need Open
/Close
here - Dapper is happy to do that for you)
Upvotes: 1