Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e) //save
        {
            System.Data.SqlServerCe.SqlCeConnection  con = new System.Data.SqlServerCe.SqlCeConnection();
            System.Data.SqlServerCe.SqlCeDataAdapter daHangmanDB;
            DataRow drow;
            DataSet dsSavedGames      = new DataSet();
            DataSet dsSavedGame_Words = new DataSet();

            con.ConnectionString = "Data Source=HangmanDB.sdf";

            con.Open();
            daHangmanDB = new System.Data.SqlServerCe.SqlCeDataAdapter("SELECT * From SavedGames", con);
            daHangmanDB.Fill(dsSavedGames, "SavedGames");
            con.Close();

            if (dsSavedGames.Tables["SavedGames"].Rows.Count > 0)
            {
                drow     = dsSavedGames.Tables["SavedGames"].Rows[dsSavedGames.Tables["SavedGames"].Rows.Count - 1];
                latestID = System.Convert.ToInt32(drow[0]) + 1;
            }
            else
            {
                latestID = 0;
            }

            drow    = dsSavedGames.Tables["SavedGames"].NewRow();
            drow[1] = textBox1.Text;
            drow[2] = wins;
            drow[3] = losses;
            drow[4] = DateTime.Now;
            dsSavedGames.Tables["SavedGames"].Rows.Add(drow);

            con.Open();
            System.Data.SqlServerCe.SqlCeCommandBuilder cb;
            cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(daHangmanDB);
            cb.DataAdapter.Update(dsSavedGames.Tables["SavedGames"]);
            con.Close();

            /*---------------------------------------------------------------------*/

            con.Open();
            daHangmanDB = new System.Data.SqlServerCe.SqlCeDataAdapter("SELECT * From SavedGame_Words", con);
            daHangmanDB.Fill(dsSavedGame_Words, "SavedGame_Words");
            con.Close();

            foreach (int id in wordsplayedimported)
            {
                drow    = dsSavedGame_Words.Tables["SavedGame_Words"].NewRow();
                drow[0] = latestID;
                drow[1] = id;
                dsSavedGame_Words.Tables["SavedGame_Words"].Rows.Add(drow);
            }

            con.Open();
            cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(daHangmanDB);
            cb.DataAdapter.Update(dsSavedGame_Words.Tables["SavedGame_Words"]);
            con.Close();

            MessageBox.Show("Game Saved");
        }
Esempio n. 2
0
        private void button3_Click(object sender, EventArgs e) //Loading a new wordlist
        {
            OpenFileDialog loadlist = new OpenFileDialog();

            loadlist.Filter           = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            loadlist.FilterIndex      = 1;
            loadlist.Multiselect      = false;
            loadlist.InitialDirectory = "";

            if (loadlist.ShowDialog() == DialogResult.OK)   //user clicked ok
            {
                String Filepath = loadlist.FileName;
                System.IO.StreamReader inpfile = new System.IO.StreamReader(Filepath);


                System.Data.SqlServerCe.SqlCeConnection  con = new System.Data.SqlServerCe.SqlCeConnection();
                System.Data.SqlServerCe.SqlCeDataAdapter daHangmanDB;
                DataRow drow;
                DataSet dsWordlist = new DataSet();
                con.ConnectionString = "Data Source=HangmanDB.sdf";

                con.Open();
                SqlCeCommand cmd = con.CreateCommand();
                cmd.CommandText = "DELETE FROM Wordlist";
                cmd.ExecuteNonQuery();

                daHangmanDB = new System.Data.SqlServerCe.SqlCeDataAdapter("SELECT * From Wordlist", con);
                daHangmanDB.Fill(dsWordlist, "Wordlist");
                con.Close();

                String data, category, words;
                while ((data = inpfile.ReadLine()) != null)
                {
                    category = data.Split(',')[0];
                    words    = data.Split(',')[1];
                    drow     = dsWordlist.Tables["Wordlist"].NewRow();
                    drow[1]  = category;
                    drow[2]  = words;
                    dsWordlist.Tables["Wordlist"].Rows.Add(drow);
                }

                con.Open();
                System.Data.SqlServerCe.SqlCeCommandBuilder cb;
                cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(daHangmanDB);
                cb.DataAdapter.Update(dsWordlist.Tables["Wordlist"]);
                con.Close();
                MessageBox.Show("Database Updated.");
            }
        }
Esempio n. 3
0
        private void button2_Click(object sender, EventArgs e) //delete
        {
            System.Data.SqlServerCe.SqlCeConnection  con = new System.Data.SqlServerCe.SqlCeConnection();
            System.Data.SqlServerCe.SqlCeDataAdapter daHangmanDB;
            DataRow drow;
            DataSet dsSavedGame_Words = new DataSet();

            con.ConnectionString = "Data Source=HangmanDB.sdf";

            con.Open();
            daHangmanDB = new System.Data.SqlServerCe.SqlCeDataAdapter("SELECT * From SavedGame_Words", con);
            daHangmanDB.Fill(dsSavedGame_Words, "SavedGame_Words");
            con.Close();

            int selectedID = System.Convert.ToInt32(listView1.SelectedItems[0].SubItems[0].Text);

            for (int i = 0; i < dsSavedGame_Words.Tables["SavedGame_Words"].Rows.Count; ++i)
            {
                drow = dsSavedGame_Words.Tables["SavedGame_Words"].Rows[i];
                if (System.Convert.ToInt32(drow[0]) == selectedID)
                {
                    dsSavedGame_Words.Tables["SavedGame_Words"].Rows[i].Delete(); //Marks row for deletion (does not remove)
                }
            }

            con.Open();
            SqlCeCommand cmd = con.CreateCommand();

            cmd.CommandText = "DELETE FROM SavedGame_Words";
            cmd.ExecuteNonQuery();
            con.Close();

            con.Open();
            System.Data.SqlServerCe.SqlCeCommandBuilder cb;
            cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(daHangmanDB);
            cb.DataAdapter.Update(dsSavedGame_Words.Tables["SavedGame_Words"]);
            con.Close();
            MessageBox.Show("Database Updated.");
            con.Close();

            con.Open();
            cmd             = con.CreateCommand();
            cmd.CommandText = "DELETE FROM SavedGames WHERE ID=" + System.Convert.ToString(selectedID);
            cmd.ExecuteNonQuery();
            con.Close();

            listView1.Items.Remove(listView1.SelectedItems[0]);
        }