SoFunction
Updated on 2025-03-07

C# interface example code for operating SQL server2008

First, query the entire table, use combobox to select which table to query, and finally use DataGridView to display it

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
namespace WindowsFormsApplication2 
{ 
  public partial class Form1 : Form 
  { 
    public Form1() 
    { 
      InitializeComponent(); 
    } 
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
      this. = false; 
      this. = false; 
      this. = true; 
      this. = ; 
      // this. =0; 
      string sql = "select * from student"; 
      DataTable table = (sql); 
      this. = table; 
      ("Student Table"); 
      ("Teacher Forum"); 
    } 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
      string sql = ""; 
      switch (this.) 
      {  
        case 0: 
          sql = "select id as student number, name as name, sage as age from 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 = (sql); 
      this. = table; 
    } 
  } 
} 

Then modify the table. This is relatively simple, using textbox and button

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
namespace WindowsFormsApplication2 
{ 
  public partial class Form2 : Form 
  { 
    public Form2() 
    { 
      InitializeComponent(); 
    } 
    private void button4_Click(object sender, EventArgs e) 
    { 
      (); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      string sql = ("insert into teacher values('{0}','{1}','{2}')", 
              this., this., this.); 
      (sql); 
    } 
    private void button2_Click(object sender, EventArgs e) 
    { 
      string sql = ("update teacher set ('{0}',''{1}'','{2}')", 
              this., this., this.); 
      (sql); 
    } 
    private void button3_Click(object sender, EventArgs e) 
    { 
      string sql = ("delete from teacher where t_id='{0}'", 
              this.); 
      (sql); 
    } 
    private void Form2_Load(object sender, EventArgs e) 
    { 
    } 
  } 
} 

Query the form by condition, this is the core, using radiobutt, combobox, button, DataGridView

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
namespace WindowsFormsApplication2 
{ 
  public partial class Form3 : Form 
  { 
    public Form3() 
    { 
      InitializeComponent(); 
    } 
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
    } 
    private void Form3_Load(object sender, EventArgs e) 
    { 
      this. = false; 
      this. = false; 
      this. = false; 
      this. = false; 
      //Initialize the teacher number      string sql = "select t_id from teacher"; 
      DataTable table = (sql); 
      string t_id; 
      foreach (DataRow row in ) 
      { 
        t_id = row["t_id"].ToString(); 
        this.(t_id); 
      } 
      if ( > 0) 
      { 
        this. = 0; 
      } 
      //Initialize the teacher's name      string sql_name = "select t_name from teacher"; 
      (); 
      table = (sql_name); 
      string t_name; 
      foreach (DataRow row in ) 
      { 
        t_name= row["t_name"].ToString(); 
        this.(t_name); 
      } 
      if ( > 0) 
      { 
        this. = 0; 
      } 
      //Initialize the student      string sql_id = "select id from student"; 
      (); 
      table = (sql_id); 
      string s_id; 
      foreach (DataRow row in ) 
      { 
        s_id = row["id"].ToString(); 
        this.(s_id); 
      } 
      if ( > 0) 
      { 
        this. = 0; 
      } 
      //Initialize the student      string sql_sname = "select name from student"; 
      (); 
      table = (sql_sname); 
      string t_sname; 
      foreach (DataRow row in ) 
      { 
        t_sname = row["name"].ToString(); 
        this.(t_sname); 
      } 
      if ( > 0) 
      { 
        this. = 0; 
      } 
    } 
    private void button2_Click(object sender, EventArgs e) 
    { 
      (); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      string sql = ""; 
      if (this.) 
      { 
        sql = ("select t_id as Teacher number,t_name as Teacher's name,t_age as age from teacher where t_id = '{0}'", 
          this.); 
      } 
      else if (this.) 
      { 
        sql = ("select t_id as Teacher number,t_name as Teacher's name,t_age as age from teacher where t_name = '{0}'", 
          this.); 
      } 
      else if (this.) 
      { 
        sql = ("select id as Student number,name as Student name,sage as age from student where id = '{0}'", 
          this.); 
      } 
      else if (this.) 
      { 
        sql = ("select id as Student number,name as Student name,sage as age from student where name = '{0}'", 
          this.); 
      } 
      DataTable table = (sql); 
      if ( > 0) 
      { 
        this. = table; 
      } 
      else 
      { 
        ("No related content"); 
      } 
    } 
    private void radioButton1_CheckedChanged(object sender, EventArgs e) 
    { 
      if (this.) 
      { 
        this. = true; 
      } 
      else 
      { 
        this. = false; 
      } 
    } 
    private void radioButton2_CheckedChanged(object sender, EventArgs e) 
    { 
      if (this.) 
      { 
        this. = true; 
      } 
      else 
      { 
        this. = false; 
      } 
    } 
    private void radioButton3_CheckedChanged(object sender, EventArgs e) 
    { 
      if (this.) 
      { 
        this. = true; 
      } 
      else 
      { 
        this. = false; 
      } 
    } 
    private void radioButton4_CheckedChanged(object sender, EventArgs e) 
    { 
      if (this.) 
      { 
        this. = true; 
      } 
      else 
      { 
        this. = false; 
      } 
    } 
  } 
} 

The above is the interface example code for c# operation SQL server2008 introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!