@Chuck mentions EntityFramework, which simplifies the work and does all the work of writing sql for you.
But there is a basic ADO.NET approach, which I will describe below.
Classes follow standard templates, so there are exact replica classes for inserting / reading from SQL server or other databases, such as SqlConnection or OleDbConnection and OleDbCommand , etc.
This is the easiest approach to ado.net:
using( SqlCeConnection conn = new SqlCeConnection(@"Data Source=|DataDirectory|\dbJournal.sdf") ) using( SqlCeCommand cmd = conn.CreateCommand() ) { conn.Open();
Then to read the data:
while (rd.Read()) {//loop through the records one by one //0 gets the first columns data for this record //as an INT rd.GetInt32(0); //gets the second column as a string rd.GetString(1); }
A good and fast way to read data is as follows:
using( SqlCeDataAdapter adap = new SqlCeDataAdapter("SELECT * FROM tblJournal", "your connection") ) {
This allows you to get all the data in one shot into the DataTable class.
Insert data:
SqlCeCommand cmdInsert = conn.CreateCommand(); cmdInsert.CommandText = "INSERT TO tblJournal (column1, column2, column2) VALUES (value1, value2, value3)"; cmdInsert.ExecuteNonQuery();
gideon
source share