Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
         {
             try
             {
                 var _with1 = openFileDialog1;
                 _with1.Filter = ("DB Backup File|*.bak;");
                 _with1.FilterIndex = 4;
                 //Clear the file name
                 openFileDialog1.FileName = "";

                 if (openFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     Cursor = Cursors.WaitCursor;
                     timer2.Enabled = true;
                     SqlConnection.ClearAllPools();
                     con = new SqlConnection(cs.DBConn);
                     con.Open();
                     string cb = "USE Master ALTER DATABASE [" + System.Windows.Forms.Application.StartupPath + "\\pos_db.mdf] SET Single_User WITH Rollback Immediate Restore database [" + System.Windows.Forms.Application.StartupPath + "\\pos_db.mdf] FROM disk='" + openFileDialog1.FileName + "' WITH REPLACE ALTER DATABASE [" + System.Windows.Forms.Application.StartupPath + "\\pos_db.mdf] SET Multi_User ";
                     cmd = new SqlCommand(cb);
                     cmd.Connection = con;
                     cmd.ExecuteReader();
                     MessageBox.Show("Successfully performed", "Database Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     GetData();
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }