欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#操作sql server2008 的界面实例代码

程序员文章站 2022-07-19 16:36:44
先是查询整张表,用到combobox选择查询哪张表,最后用datagridview显示 using system; using system.collecti...

先是查询整张表,用到combobox选择查询哪张表,最后用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() 
    { 
      initializecomponent(); 
    } 
    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("学生表"); 
      combobox1.items.add("教师表"); 
    } 
    private void combobox1_selectedindexchanged(object sender, eventargs e) 
    { 
      string sql = ""; 
      switch (this.combobox1.selectedindex) 
      {  
        case 0: 
          sql = "select id as 学生号,name as 姓名,sage as 年龄 from student"; 
          break; 
        case 1: 
          sql = "select t_id as 教师号,t_name as 姓名,t_age as 年龄 from teacher"; 
          break; 
        default: 
          break; 
      } 
      datatable table = sqlmanage.tableselect(sql); 
      this.datagridview1.datasource = table; 
    } 
  } 
} 

然后是修改表格,这个比较简单,用到textbox和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() 
    { 
      initializecomponent(); 
    } 
    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.textbox3.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.text); 
      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) 
    { 
    } 
  } 
} 

按条件查询表格,这个是核心,用到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() 
    { 
      initializecomponent(); 
    } 
    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; 
      //初始化教师编号 
      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; 
      } 
      //初始化教师姓名 
      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; 
      } 
      //初始化学生 
      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; 
      } 
      //初始化学生 
      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 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_id = '{0}'", 
          this.combobox1.text); 
      } 
      else if (this.radiobutton2.checked) 
      { 
        sql = string.format("select t_id as 教师编号,t_name as 教师姓名,t_age as 年龄 from teacher where t_name = '{0}'", 
          this.combobox2.text); 
      } 
      else if (this.radiobutton3.checked) 
      { 
        sql = string.format("select id as 学生编号,name as 学生姓名,sage as 年龄 from student where id = '{0}'", 
          this.combobox3.text); 
      } 
      else if (this.radiobutton4.checked) 
      { 
        sql = string.format("select id as 学生编号,name as 学生姓名,sage as 年龄 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("没有相关内容"); 
      } 
    } 
    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; 
      } 
    } 
  } 
} 

以上所述是小编给大家介绍的c#操作sql server2008 的界面实例代码,希望对大家有所帮助