Click here to Skip to main content
15,896,154 members
Articles / Mobile Apps / Windows Mobile

Pocket Personal Health Record

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
17 Feb 2009Ms-PL2 min read 28.7K   1.1K   24  
Personal Pocket Health Record (PPHR) application helps to store and track a user's personal details and visits information on Windows powered pocket PC.
//**************************************************************************//
// Copyright (C) Abdul Rasheed. All rights Reserved.                        //
// rasheedat.blogspot.com                                                   //
//**************************************************************************//
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;
namespace PPHR.DataAccess
{
    /// <summary>
    /// This class helps to connect to SQL CE db to get data
    /// </summary>
    public class DBConnection
    {
        string connectionString = string.Empty;
        string appPath = string.Empty;
        private SqlCeConnection conn;
       
        public DBConnection()
        {
            appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        }
       
        private void Connect()
        {
            connectionString = @"Data Source=" + appPath +"\\PPHRData.sdf";
            if (conn == null || conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
            {
                conn = new SqlCeConnection(connectionString);
                conn.Open();
            }
        }
        
        private void Disconnect()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }

        /// <summary>
        /// Execute the SQL statement and return dataset
        /// </summary>
        /// <param name="sSql"></param>
        /// <returns></returns>
        public DataTable ExecuteQueryAndGetDataTable(string sSql)
        {
            DataTable dt=null;
            DataSet ds = new DataSet();
            try
            {
                Connect();
                SqlCeDataAdapter da = new SqlCeDataAdapter(sSql, conn);
                da.Fill(ds);
                if (ds.Tables.Count >= 0)
                {
                    dt = ds.Tables[0];
                }
                Disconnect();
            }
            catch(Exception ex)
            {
                throw new Exception("DBConnection", ex);
            }
            return dt;
        }

        /// <summary>
        /// Execute the sql statement and return the commited records count
        /// </summary>
        /// <param name="sSql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sSql)
        {
            int commitedRows = -1;
            try
            {
                Connect();
                SqlCeCommand cmd = new SqlCeCommand(sSql, conn);
                commitedRows = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("DBConnection", ex);
            }
            return commitedRows;
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
India India
Called as Rasheed. Completed Master of Computer science. Working as Senior Consultant in Chennai, India.

Try to achive in different stream

Comments and Discussions