Fill datatable from access database
Hi to all.
In previous post we show how to execute query in database. now in this post we see how to get data from database. and fill in DataTable.
for this we create on function in dbhelper class which we use in previous example.
public DataTable FillDataTable(string sQry)
{
try
{
DataTable Dt = new DataTable();
//Check db connection
if (dbConnection.State != ConnectionState.Open)
{
dbConnection.Open();
}
//create command
OleDbCommand cmd = new OleDbCommand(sQry, dbConnection);
//create OleDbDataAdapter for execute and get data
OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
//fill datatable form data adapter
ad.Fill(Dt);
return Dt;
}
catch (Exception ex)
{
throw ex;
}
}
This function return a datatable which contain data of database.
Now we call the function
First create query
string sQry="Select * from employee";
Datatable Dt =FillDataTable(sQry);
This code can help you to fill datatable from database.
Thanks to read this post.
Comments
Post a Comment