⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addressbook.cs

📁 this is a good book for the visual c#
💻 CS
📖 第 1 页 / 共 4 页
字号:
         }
      
         catch ( System.Data.OleDb.OleDbException oleException )
         {
            Console.WriteLine( oleException.StackTrace );
            statusTextBox.Text += oleException.ToString();
         }
      
         catch ( InvalidOperationException invalidException )
         {
            MessageBox.Show( invalidException.Message );
         }
      
      }  // end of findButton_Click

      private void addButton_Click(object sender, System.EventArgs e)
      {
         try
         {
            if ( lastTextBox.Text != "" && firstTextBox.Text != "" )
            {
               // create the SQL query to insert a row
               oleDbDataAdapter1.InsertCommand.CommandText = 
                  "INSERT INTO addresses (" +
                  "firstname, lastname, address, city, " +
                  "stateorprovince, postalcode, country, " +
                  "emailaddress, homephone, faxnumber" + 
                  ") VALUES ('" +
                  firstTextBox.Text + "', '" +
                  lastTextBox.Text + "', '" +
                  addressTextBox.Text + "', '" +
                  cityTextBox.Text + "', '" +
                  stateTextBox.Text + "', '" +
                  postalTextBox.Text + "', '" +
                  countryTextBox.Text + "', '" +
                  emailTextBox.Text + "', '" +
                  homeTextBox.Text + "', '" +
                  faxTextBox.Text + "')";

               // notify the user the query is being sent
               statusTextBox.Text += "\r\nSending query: " +
                  oleDbDataAdapter1.InsertCommand.CommandText +
                  "\r\n" ;

               // send query
               oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

               statusTextBox.Text += "\r\nQuery successful\r\n";
            }
         
            else
               statusTextBox.Text += "\r\nEnter at least first " +
                  "and last name then press Add\r\n";
         }
      
         catch ( System.Data.OleDb.OleDbException oleException )
         {
            Console.WriteLine( oleException.StackTrace );
            statusTextBox.Text += oleException.ToString();
         }
      
      }  // end of addButton_Click

      private void updateButton_Click(object sender, System.EventArgs e)
      {
         try
         {
            // make sure the user has already found the record
            // he or she wishes to update
            if ( idTextBox.Text != "" )
            {
               // set the SQL query to update all the fields in
               // the table where the id number matches the id
               // in idTextBox
               oleDbDataAdapter1.UpdateCommand.CommandText =
                  "UPDATE addresses SET " +
                  "firstname ='" + firstTextBox.Text +
                  "', lastname='" + lastTextBox.Text +
                  "', address='" + addressTextBox.Text +
                  "', city='" + cityTextBox.Text +
                  "', stateorprovince='" + stateTextBox.Text +
                  "', postalcode='" + postalTextBox.Text +
                  "', country='" + countryTextBox.Text +
                  "', emailaddress='" + emailTextBox.Text +
                  "', homephone='" + homeTextBox.Text +
                  "', faxnumber='" + faxTextBox.Text +
                  "' WHERE id=" + idTextBox.Text;

               // notify the user the query is being set
               statusTextBox.Text += "\r\nSending query: " +
                  oleDbDataAdapter1.UpdateCommand.CommandText +
                  "\r\n";

               // execute query
               oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery();

               statusTextBox.Text += "\r\nQuery successful\r\n";
            }
         
            else
               statusTextBox.Text += "\r\nYou may only update " +
                  "an existing record. Use Find to locate the" +
                  "record, then modify the information and " +
                  "press Update.\r\n";
         }
      
         catch ( System.Data.OleDb.OleDbException oleException )
         {
            Console.WriteLine( oleException.StackTrace );
            statusTextBox.Text += oleException.ToString();
         }
      
      } // end of updateButton_Click

      private void clearButton_Click(object sender, System.EventArgs e)
      {
         idTextBox.Clear();
         ClearTextBoxes();
      }

      private void helpButton_Click(object sender, System.EventArgs e)
      {
         statusTextBox.AppendText( 
            "\r\nClick Find to locate a record\r\n" +
            "Click Add to insert a new record.\r\n" +
            "Click Update to update the information in a record "
            + "\r\nClick Clear to empty the textboxes" );
      }
	   
      public void Display( DataSet dataSet )
      {
         try
         {   
            // get the first DataTable - there will always be one
            DataTable dataTable = dataSet.Tables[ 0 ];
         
            if ( dataTable.Rows.Count != 0 )
            {
               int recordNumber = (int)dataTable.Rows[ 0 ][ 0 ];

               idTextBox.Text = recordNumber.ToString();
               firstTextBox.Text = 
                  (string)dataTable.Rows[ 0 ][ 1 ];
               lastTextBox.Text = (
                  string)dataTable.Rows[ 0 ][ 2 ];
               addressTextBox.Text = 
                  (string)dataTable.Rows[ 0 ][ 3 ];
               cityTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 4 ];
               stateTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 5 ];
               postalTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 6 ];
               countryTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 7 ];
               emailTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 8 ];
               homeTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 9 ];
               faxTextBox.Text =
                  (string)dataTable.Rows[ 0 ][ 10 ];
            }
         
            else
               statusTextBox.Text += "\r\nNo record found\r\n";
         }
      
         catch( System.Data.OleDb.OleDbException oleException )
         {
            Console.WriteLine( oleException.StackTrace );
            statusTextBox.Text += oleException.ToString();
         }
      
      } // end of Display

      public void ClearTextBoxes()
      {
         firstTextBox.Clear();
         lastTextBox.Clear();
         addressTextBox.Clear();
         cityTextBox.Clear();
         stateTextBox.Clear();
         postalTextBox.Clear();
         countryTextBox.Clear();
         emailTextBox.Clear();
         homeTextBox.Clear();
         faxTextBox.Clear();
      }
   }
}

/*
 **************************************************************************
 * (C) Copyright 2002 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -