Detailed interface Instance code for C # operations SQL server2008

Source: Internet
Author: User
This article mainly introduces C # operation SQL Server2008 Interface instance code, very good, with reference value, the need for friends can refer to the following

First query the entire table, use the ComboBox Select which table to query, and finally display with DataGridView


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; namespace WindowsFormsApplication2 {public partial class Form1:form {public Form1 () {Initializecomp     Onent ();  private void Datagridview1_cellcontentclick (object sender, DataGridViewCellEventArgs e) {} private void       Form1_Load (object sender, EventArgs e) {this.dataGridView1.RowHeadersVisible = false;       This.dataGridView1.AllowUserToAddRows = false;       This.dataGridView1.ReadOnly = true;       This.dataGridView1.SelectionMode = Datagridviewselectionmode.fullrowselect;       This.comboBox1.SelectedIndex = 0;       String sql = "SELECT * from student";       DataTable table = sqlmanage.tableselect (SQL);       This.dataGridView1.DataSource = table;       COMBOBOX1.ITEMS.ADD ("Student table");     COMBOBOX1.ITEMS.ADD ("Teachers ' table"); } private void Combobox1_SelectedIndexChanged (object sender, EventArgs e) {String sql = ""; Switch (this.comboBox1.SelectedIndex) {Case 0:sql = "SELECT ID as student number, name as name, Sage as age fro           M student ";         Break           Case 1:sql = "Select t_id as teacher number, t_name as name, T_age as age from teacher";         Break       Default:break;       } DataTable table = Sqlmanage.tableselect (SQL);     This.dataGridView1.DataSource = table; }   } }

Then there is the change of the table, this is relatively simple, use the TextBox and button


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; namespace WindowsFormsApplication2 {public partial class Form2:form {public Form2 () {Initializecomp     Onent (); private void Button4_Click (object sender, EventArgs e) {this.     Close (); private void Button1_Click (object sender, EventArgs e) {String sql = string. Format ("INSERT into teacher values (' {0} ', ' {1} ', ' {2} ')", This.textBox1.Text, This.textBox2.Text, This.textbox       3.Text);     Sqlmanage.tablechange (SQL); private void Button2_Click (object sender, EventArgs e) {String sql = string. Format ("Update teacher set (' {0} ', ' {1} ', ' {2} ')", This.textBox1.Text, This.textBox2.Text, This.textBox3.Tex       T);     Sqlmanage.tablechange (SQL);     private void Button3_Click (object sender, EventArgs e) {  String sql = string.       Format ("Delete from teacher where t_id= ' {0} '", This.textBox1.Text);     Sqlmanage.tablechange (SQL); private void Form2_load (object sender, EventArgs e) {}}}

Query the table by condition, this is the core, use to Radiobutt,combobox,,button, DataGridView


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; namespace WindowsFormsApplication2 {public partial class Form3:form {public Form3 () {Initializecomp     Onent ();  private void Datagridview1_cellcontentclick (object sender, DataGridViewCellEventArgs e) {} private void       Form3_load (object sender, EventArgs e) {this.comboBox1.Enabled = false;       this.comboBox2.Enabled = false;       this.comboBox3.Enabled = false;       this.comboBox4.Enabled = false;       Initialize teacher number String sql = "Select t_id from Teacher";       DataTable table = sqlmanage.tableselect (SQL);       String t_id; foreach (DataRow row in table. Rows) {t_id = row["t_id"].         ToString ();       THIS.COMBOBOX1.ITEMS.ADD (t_id); } if (table.      Rows.Count > 0) {this.comboBox1.SelectedIndex = 0; }//Initialize teacher name string sql_name = "Select T_name from Teacher"; Table.       Clear ();       Table = Sqlmanage.tableselect (sql_name);       String T_name; foreach (DataRow row in table. Rows) {t_name= row["T_name"].         ToString ();       THIS.COMBOBOX2.ITEMS.ADD (T_name); } if (table.       Rows.Count > 0) {this.comboBox2.SelectedIndex = 0;       }//Initialize student string sql_id = "SELECT id from student"; Table.       Clear ();       Table = Sqlmanage.tableselect (sql_id);       String s_id; foreach (DataRow row in table. Rows) {s_id = row["id"].         ToString ();       THIS.COMBOBOX3.ITEMS.ADD (s_id); } if (table.       Rows.Count > 0) {this.comboBox3.SelectedIndex = 0;       }//Initialize student string sql_sname = "SELECT name from Student"; Table.       Clear ();       Table = Sqlmanage.tableselect (sql_sname);       String T_sname; foreach (DataRow row in table. Rows) {t_sname = row["Name"].         ToString ();       THIS.COMBOBOX4.ITEMS.ADD (T_sname); } if (table.       Rows.Count > 0) {this.comboBox4.SelectedIndex = 0; }} private void Button2_Click (object sender, EventArgs e) {this.     Close ();       private void Button1_Click (object sender, EventArgs e) {String sql = ""; if (this.radioButton1.Checked) {sql = string.       Format ("Select t_id as teacher number, t_name as teacher name, T_age as age from teacher where t_id = ' {0} '", This.comboBox1.Text); } else if (this.radioButton2.Checked) {sql = string.        Format ("Select t_id as teacher number, t_name as teacher name, T_age as age from teacher where T_name = ' {0} '", This.comboBox2.Text); } else if (this.radioButton3.Checked) {sql = string.       Format ("SELECT ID as student number, name as student name, Sage as age from student where id = ' {0} '", This.comboBox3.Text); } else if (this.radioButton4.Checked) {sQL = string.       Format ("SELECT ID as student number, name as student name, Sage as age from student where name = ' {0} '", This.comboBox4.Text);       } DataTable table = Sqlmanage.tableselect (SQL); if (table.       Rows.Count > 0) {this.dataGridView1.DataSource = table;       } else {MessageBox.Show ("No related content"); }} private void Radiobutton1_checkedchanged (object sender, EventArgs e) {if (this.radioButton1.Checked       ) {this.comboBox1.Enabled = true;       } else {this.comboBox1.Enabled = false; }} private void Radiobutton2_checkedchanged (object sender, EventArgs e) {if (this.radioButton2.Checked       ) {this.comboBox2.Enabled = true;       } else {this.comboBox2.Enabled = false; }} private void Radiobutton3_checkedchanged (object sender, EventArgs e) {if (this.radioButton3.Checked ) {this.comboBox3.Enabled = tRue       } else {this.comboBox3.Enabled = false; }} private void Radiobutton4_checkedchanged (object sender, EventArgs e) {if (this.radioButton4.Checked       ) {this.comboBox4.Enabled = true;       } else {this.comboBox4.Enabled = false; }     }   } }
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.