Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to View Data from MS Access in Data Grid View?

I am new in C-Sharp, i am trying to access my Database from C-Sharp, i have written the following code, and i dont know what to write next to view data. I have searched this on net but didnt get much. Kindly tell me this in easy code.

string connection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database3.accdb";

OleDbConnection conn = new OleDbConnection(connection);
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * from score", conn);

OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.SelectCommand = cmd;
like image 613
Taha Kirmani Avatar asked Oct 26 '25 12:10

Taha Kirmani


1 Answers

Refer following code:

string strProvider = "@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database3.accdb";
string strSql = "Select * from score";
OleDbConnection con = new OleDbConnection(strProvider);
OleDbCommand cmd = new OleDbCommand(strSql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable scores = new DataTable();
da.Fill(scores);
dataGridView1.DataSource = scores;

Hope its helpful.

like image 142
Freelancer Avatar answered Oct 28 '25 02:10

Freelancer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!