Vinit Patel

Archive for the ‘Windows Mobile Aplication Development’ Category

Here, I will explain you about windows mobile application development.

In that I will first explain you about database connectivity with application.

For database , Sql server Compact Edition, or say sql server Mobile Edition is used.

First of all create database TestDataBase.sdf

1)      How to Create database ???

string connStr = (“Data Source = TestDataBase.sdf”);

SqlCeEngine engine = new SqlCeEngine(connStr);

engine.CreateDatabase();

2)    If database is already created then ??

string FileName = “TestDataBase.sdf”;

if (File.Exists(FileName))

{

File.Delete(FileName);

}

3)    After database creation, create a table, how ??

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = conn.CreateCommand();

cmd.CommandText = “CREATE TABLE TestTable (Id int, FirstName

ntext, LastName ntext)”;

SqlCeTransaction trans = conn.BeginTransaction

(IsolationLevel.ReadCommitted);

cmd.Transaction = trans;

cmd.ExecuteNonQuery();

trans.Commit(CommitMode.Immediate);

MessageBox.Show(“Table Created Successfully.”);

Here,    Button 1 = Data Display Button,

Button 2 = Data Insert Button,

Button 3 = Data Select From DataGrid Button,

Button 4 = Data Update Button,

DataGrid1 = DataGrid.

4)    After Table Creation , (insert,update,select) process

Data Insert Code : =

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = null;

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

cmd = null;

if (String.IsNullOrEmpty(txtFirstName.Text) && String.IsNullOrEmpty(txtLastName.Text))

{

MessageBox.Show(“Enter The First Or Last Name”);

}

else

{

string FName = Convert.ToString(txtFirstName.Text);

string LName = Convert.ToString(txtLastName.Text);

int Result = InsertData(FName, LName);

if (Result > 0)

{

MessageBox.Show(“Record Inserted Successfully.”);

}

txtFirstName.Text = string.Empty;

txtLastName.Text = string.Empty;

}

InsertData Function Code

private int InsertData(string FirstName, string LastName)

{

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = null;

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

int Id = Convert.ToInt32(label3.Text)+ 1;

string Query = “insert into TestTable (Id, FirstName, LastName) values (@Id, @FirstName, @LastName)”;

cmd = new SqlCeCommand(Query, conn);

cmd.Parameters.AddWithValue(“@Id”, Id);

cmd.Parameters.AddWithValue(“@FirstName”, FirstName);

cmd.Parameters.AddWithValue(“@LastName”, LastName);

trans = conn.BeginTransaction();

cmd.Transaction = trans;

int Result = cmd.ExecuteNonQuery();

trans.Commit(CommitMode.Immediate);

label3.Text = Convert.ToString(Id);

return Result;

}

5)    Display Data in DataGrid from database.

Data Display Code : =

DataTable dt = GetData();

if (dt.Rows.Count > 0)

{

dataGrid1.DataSource = dt;

}

else

{

MessageBox.Show(“No Records.”);

}

GetData Function Code

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

SqlCeCommand cmd = new SqlCeCommand(“select * from TestTable”, conn);

SqlCeDataAdapter adp = new SqlCeDataAdapter(cmd);

DataTable dt = new DataTable();

adp.Fill(dt);

return dt;

6)    Update Data for selected data in Datagrid in to Database.

Data Select From DataGrid :=

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = null;

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

button1.Enabled = false;

button2.Enabled = false;

dataGrid1.Select(Convert.ToInt32(dataGrid1.CurrentRowIndex));

int RowId = Convert.ToInt32(dataGrid1.CurrentRowIndex);

DataTable dt = GetData();

txtFirstName.Text = Convert.ToString (dt.Rows[RowId][“FirstName”]);

txtLastName.Text = Convert.ToString(dt.Rows[RowId][“LastName”]);

label3.Text = Convert.ToString(dt.Rows[RowId][“Id”]);

button4.Enabled = false;

GetData Function Code

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

SqlCeCommand cmd = new SqlCeCommand(“select * from TestTable”, conn);

SqlCeDataAdapter adp = new SqlCeDataAdapter(cmd);

DataTable dt = new DataTable();

adp.Fill(dt);

return dt;

7)    Update Data Which is to be first Select from DataGrid.

Data Update in DataGrid as well as Database :=

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = null;

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

if (String.IsNullOrEmpty(txtFirstName.Text) && String.IsNullOrEmpty(txtLastName.Text))

{

MessageBox.Show(“Enter The First Or Last Name”);

}

else

{

int Id = Convert.ToInt32(label3.Text);

string FName = Convert.ToString(txtFirstName.Text);

string LName = Convert.ToString(txtLastName.Text);

int Result = UpdateData(Id, FName,LName);

if (Result > 0)

{

MessageBox.Show(“Record Updated Successfully.”);

}

DataTable dt = GetData();

dataGrid1.DataSource = dt;

button1.Enabled = true;

button2.Enabled = true;

button4.Enabled = true;

txtFirstName.Text = string.Empty;

txtLastName.Text = string.Empty;

}

UpdateData Function Code

SqlCeConnection conn = new SqlCeConnection(connStr);

conn.Open();

SqlCeCommand cmd = null;

SqlCeTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

string Query = “Update TestTable set FirstName=@FirstName,LastName=@LastName Where Id=@Id”;

cmd = new SqlCeCommand(Query, conn);

cmd.Parameters.AddWithValue(“@Id”, Id);

cmd.Parameters.AddWithValue(“@FirstName”, FirstName);

cmd.Parameters.AddWithValue(“@LastName”, LastName);

trans = conn.BeginTransaction();

cmd.Transaction = trans;

int Result = cmd.ExecuteNonQuery();

trans.Commit(CommitMode.Immediate);

return Result;

Advertisements

Calendar

November 2017
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
27282930  

Stats Of Blog

  • 7,054 hits

Cluster Map