In this Part-VI blog series we are going to use ADO.NET to program against SQL CE. SQL CE Database is a file based DB not like Desktop MS-SQL server (Windows Service),so to access DB File In windows mobile there is no such thing as a current directory (like C:\). for example If your app folder is Program Files\SreeniMobileApps, I'd expect my contact Database is located @ "Data Source=Program Files\SreeniMobileApps\Contacts.sdf".
To get Database file path use the following code snippet
private static stringInitializeConnectionString()
{
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
return "Data Source=" + Path.Combine(appPath, "Contacts.sdf") + " ;pwd=;";
}
Now let’s connect Contacts DB and query via ADO.NET API. (Note: The contacts Database we were created in part-V ).
I have Menu option 5 as view Contact in my mobile application when user selects that options it will shows all the contacts in DataGrid.
Here is code snippet which connect to Contacts DB and Query the Contact Table .
private void ViewContact_Load(object sender, EventArgs e)
{
string selectQuery = "Select * from Contact";
using (SqlCeConnection conn = new SqlCeConnection(InitializeConnectionString()))
{
try
{
SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(selectQuery, conn);
conn.Open();
DataTable dataTble = new DataTable("ContactData");
dataAdapter.Fill(dataTble);
dataGrid1.DataSource = dataTble;
}
catch (SqlCeException ex)
{
}
}
}
}
Result
Nandri(Thanks)
SreenivasaRagavan.
1 comment:
Hi,
i like the background picture on the address book's screenshot. May I please you to send it to me? my addess is ebuzatti@gmail.com
Thanx in advance
Post a Comment