コード例 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //
            // As app starts up, ping SQL Server to start it running:
            //

            DataAccessTier.Data datatier = new DataAccessTier.Data(dbfilename);

            try
            {
                datatier.TestConnection();
            }
            catch
            {
                // ignore since just pinging to start:
            }
            //
            //Create Linq to SQL object context
            //

            db = new CoursemoEntities();
        }
コード例 #2
0
        private void resetDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //
            // Reset the database, i.e. delete all the rentals and
            // set all bikes to *not* rented:
            //

            int            retries = 0;
            SqlConnection  db      = null;
            SqlTransaction tx      = null;

            while (retries < 3)
            {
                try
                {
                    db = new SqlConnection(connectioninfo);
                    db.Open();

                    tx = db.BeginTransaction(IsolationLevel.Serializable);

                    string sql = string.Format(@"
DELETE FROM StudentCourses;  -- delete all StudentCourses:

DELETE FROM Waitlist;  -- now delete all Waitlist items:

UPDATE Courses

Set NumEnrolled = 0;
");

                    //MessageBox.Show(sql);

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection  = db;
                    cmd.CommandText = sql;
                    cmd.Transaction = tx;

                    //MessageBox.Show(sql);

                    int rowsModified = cmd.ExecuteNonQuery();

                    //Commit Transactions
                    tx.Commit();

                    //
                    // reset the GUI:
                    //
                    cmdLoadCourses_Click(sender, e);
                    cmdLoadStudents_Click(sender, e);
                    break;
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 1205)
                    {
                        retries++;
                    }
                    else
                    {
                        MessageBox.Show(ex.Message);
                        break;
                    }
                }

                catch (Exception ex)
                {
                    if (tx != null)
                    {
                        tx.Rollback();
                    }
                    MessageBox.Show(ex.Message);
                    break;
                }
                finally
                {
                    //MessageBox.Show("This is the Finally Satement");
                    if (db != null)
                    {
                        db.Close();
                    }
                }
            }
        }