C # Access SQL Server additions and Deletions check code instances _c# Tutorial

Source: Internet
Author: User
Tags exception handling

A special implementation of access to SQL Server database and delete the operation code to check, to share with you, the specific content as follows

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Data;

Using System.Data.SqlClient; namespace WindowsFormsApplication1 {public partial class Form1:form {public Form1 () {Initializecomp
    Onent (); }//Query private void Button1_Click (object sender, EventArgs e) {string myconn = "Server=127.0.0.1;uid=s A;pwd=123654;database=libbook; Trusted_connection=no "//define database connection parameters SqlConnection myconnection = new SqlConnection (myconn);//define a data connection instance Sqlcomm and mycommand = new SqlCommand ("SELECT * from Book borrow", MyConnection); Define a database operation instruction SqlDataAdapter selectadapter = new SqlDataAdapter ();//define a data adapter Selectadapter.selectcommand = My command;//defines a data adapter's operation instruction DataSet myDataSet = new DataSet ();//define a DataSet Myconnection.open ();//Open database connection SelectA Dapter. Selectcommand.executenonquery ()//Execute dataLibrary query Instruction Myconnection.close ()//Close database Selectadapter.fill (myDataSet);//fill DataSet DataGrid1.DataSource = Mydatase
      T.tables[0];
      Datagrid1.databind ()//To populate the data table with data in a DataSet}//Add private void button2_click (object sender, EventArgs e) { String myconn = "Server=127.0.0.1;uid=sa;pwd=123654;database=libbook;"
      Trusted_connection=no ";
      SqlConnection myconnection = new SqlConnection (myconn); String Myinsert = "INSERT into book borrow (book Number, reader number, renew number) VALUES (' + convert.tostring (textbox2.text) +" ', ' "+ conver
      T.tostring (TextBox3.Text) + "', '" +convert.toint32 (textbox4.text) + "')";
      SqlCommand mycommand = new SqlCommand (Myinsert, MyConnection);
        try//exception Handling {myconnection.open ();
        Mycommand.executenonquery ();
      Myconnection.close (); The catch (Exception ex) {MessageBox.Show (ex).
      message); }//update private void Button3_Click (object sender, EventArgs e) {string myconn = "Server=127.0.0.1;uid=sa;pwd=123654;database=libbook;
      Trusted_connection=no ";
      SqlConnection myconnection = new SqlConnection (myconn);
      String myupdate = "Update book borrow set operator = '" + TextBox2.Text + "'" + "where to borrow number =" + "'" + TextBox1.Text + "'";
      SqlCommand mycommand = new SqlCommand (myupdate, MyConnection);
        try {myconnection.open ();
        Mycommand.executenonquery ();
        Myconnection.close ();
      TextBox1.Text = ""; The catch (Exception ex) {MessageBox.Show (ex).
      message); }//delete private void Button4_Click (object sender, EventArgs e) {string myconn = "server=127.0.0.1 ; Uid=sa;pwd=123654;database=libbook;
      Trusted_connection=no ";
      SqlConnection myconnection = new SqlConnection (myconn);
      String mydelete = "Delete from book borrow where borrow number =" + TextBox1.Text;
      SqlCommand mycommand = new SqlCommand (Mydelete, MyConnection);
        try {myconnection.open (); MycomMand.
        ExecuteNonQuery ();
        Myconnection.close ();
      TextBox1.Text = ""; The catch (Exception ex) {MessageBox.Show (ex).
      message);

 }
    }
  }
}

The database is as follows;


Query success in WinForm;


When inserting, because the borrowing number is the self increment, cannot insert the value, will generate itself;



Update, foreign key conflict; The inserted book number is 000999, no this book, so error;


Insert success;


Update operator for "teacher Wang";


Delete the record of loan number 31;


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.