Introduction
CDList is a useful application used to locate where your different CD or DVD are.
CDList is writen in C# using SQLCe.
Brief Code Description
public NewForm(MainForm mf, int ID) {
InitializeComponent();
conn = new SqlCeConnection ("Data Source = CDList.sdf");
parentForm = mf;
IDEdit = ID;
}
private void NewForm_Load(object sender, System.EventArgs e)
{
UpdateCmbox(); if (IDEdit != 0) {
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM tbl_CDList WHERE ID = "+IDEdit+";";
SqlCeDataReader rdr = cmd.ExecuteReader();
rdr.Read();
Title.Text = rdr.GetString(1);
Content.Text = rdr.GetString(2);
cmbType.Text = rdr.GetString(3);
cmbLocation.Text = rdr.GetString(4);
conn.Close();
}
}
private void UpdateCmbox() {
Title.Text = "";
Content.Text = "";
cmbLocation.Text = "";
cmbType.Text = "";
cmbLocation.Items.Clear();
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT Title FROM tbl_Locations ORDER BY Title ASC;";
SqlCeDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
cmbLocation.Items.Add(rdr.GetString(0));
}
cmbLocation.Refresh();
cmbType.Items.Clear();
cmd.CommandText = "SELECT Title FROM tbl_Types ORDER BY Title ASC;";
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
cmbType.Items.Add(rdr.GetString(0));
}
cmbType.Refresh();
conn.Close();
}
private void Reset_Click(object sender, System.EventArgs e)
{
UpdateCmbox();
}
private void Save_Click(object sender, System.EventArgs e)
{
conn = new SqlCeConnection ("Data Source = CDList.sdf");
conn.Open();
cmd = conn.CreateCommand();
if (IDEdit != 0) {
cmd.CommandText = "UPDATE tbl_CDList SET Title = '"+Title.Text+"',"+
"Content = '"+Content.Text+"', +
"Location = '"+cmbLocation.Text+"', Type = '"+
cmbType.Text+"' WHERE ID = "+IDEdit;
}
else //else insert mode
{
cmd.CommandText = "INSERT INTO tbl_CDList(Title, Content, Location, "+
"Type) VALUES ('"+Title.Text+"', "+
"'"+Content.Text+"', '"+cmbLocation.Text+"' ,'"+
cmbType.Text+"')";
}
cmd.ExecuteNonQuery();
conn.Close();
parentForm.UpdateLView();
UpdateCmbox();
}
Resources
MSDN Library January 2004 for how to connect to a SQLCe database.