c#操作sql server2008 的界面实例代码
先是查询整张表,用到combobox选择查询哪张表,最后用DataGridView显示
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespaceWindowsFormsApplication2 { publicpartialclassForm1:Form { publicForm1() { InitializeComponent(); } privatevoiddataGridView1_CellContentClick(objectsender,DataGridViewCellEventArgse) { } privatevoidForm1_Load(objectsender,EventArgse) { this.dataGridView1.RowHeadersVisible=false; this.dataGridView1.AllowUserToAddRows=false; this.dataGridView1.ReadOnly=true; this.dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect; //this.comboBox1.SelectedIndex=0; stringsql="select*fromstudent"; DataTabletable=SqlManage.TableSelect(sql); this.dataGridView1.DataSource=table; comboBox1.Items.Add("学生表"); comboBox1.Items.Add("教师表"); } privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse) { stringsql=""; switch(this.comboBox1.SelectedIndex) { case0: sql="selectidas学生号,nameas姓名,sageas年龄fromstudent"; break; case1: sql="selectt_idas教师号,t_nameas姓名,T_ageas年龄fromteacher"; break; default: break; } DataTabletable=SqlManage.TableSelect(sql); this.dataGridView1.DataSource=table; } } }
然后是修改表格,这个比较简单,用到textbox和button
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespaceWindowsFormsApplication2 { publicpartialclassForm2:Form { publicForm2() { InitializeComponent(); } privatevoidbutton4_Click(objectsender,EventArgse) { this.Close(); } privatevoidbutton1_Click(objectsender,EventArgse) { stringsql=string.Format("insertintoteachervalues('{0}','{1}','{2}')", this.textBox1.Text,this.textBox2.Text,this.textBox3.Text); SqlManage.TableChange(sql); } privatevoidbutton2_Click(objectsender,EventArgse) { stringsql=string.Format("updateteacherset('{0}',''{1}'','{2}')", this.textBox1.Text,this.textBox2.Text,this.textBox3.Text); SqlManage.TableChange(sql); } privatevoidbutton3_Click(objectsender,EventArgse) { stringsql=string.Format("deletefromteacherwheret_id='{0}'", this.textBox1.Text); SqlManage.TableChange(sql); } privatevoidForm2_Load(objectsender,EventArgse) { } } }
按条件查询表格,这个是核心,用到radiobutt,combobox,,button,DataGridView
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; namespaceWindowsFormsApplication2 { publicpartialclassForm3:Form { publicForm3() { InitializeComponent(); } privatevoiddataGridView1_CellContentClick(objectsender,DataGridViewCellEventArgse) { } privatevoidForm3_Load(objectsender,EventArgse) { this.comboBox1.Enabled=false; this.comboBox2.Enabled=false; this.comboBox3.Enabled=false; this.comboBox4.Enabled=false; //初始化教师编号 stringsql="selectt_idfromteacher"; DataTabletable=SqlManage.TableSelect(sql); stringt_id; foreach(DataRowrowintable.Rows) { t_id=row["t_id"].ToString(); this.comboBox1.Items.Add(t_id); } if(table.Rows.Count>0) { this.comboBox1.SelectedIndex=0; } //初始化教师姓名 stringsql_name="selectt_namefromteacher"; table.Clear(); table=SqlManage.TableSelect(sql_name); stringt_name; foreach(DataRowrowintable.Rows) { t_name=row["t_name"].ToString(); this.comboBox2.Items.Add(t_name); } if(table.Rows.Count>0) { this.comboBox2.SelectedIndex=0; } //初始化学生 stringsql_id="selectidfromstudent"; table.Clear(); table=SqlManage.TableSelect(sql_id); strings_id; foreach(DataRowrowintable.Rows) { s_id=row["id"].ToString(); this.comboBox3.Items.Add(s_id); } if(table.Rows.Count>0) { this.comboBox3.SelectedIndex=0; } //初始化学生 stringsql_sname="selectnamefromstudent"; table.Clear(); table=SqlManage.TableSelect(sql_sname); stringt_sname; foreach(DataRowrowintable.Rows) { t_sname=row["name"].ToString(); this.comboBox4.Items.Add(t_sname); } if(table.Rows.Count>0) { this.comboBox4.SelectedIndex=0; } } privatevoidbutton2_Click(objectsender,EventArgse) { this.Close(); } privatevoidbutton1_Click(objectsender,EventArgse) { stringsql=""; if(this.radioButton1.Checked) { sql=string.Format("selectt_idas教师编号,t_nameas教师姓名,t_ageas年龄fromteacherwheret_id='{0}'", this.comboBox1.Text); } elseif(this.radioButton2.Checked) { sql=string.Format("selectt_idas教师编号,t_nameas教师姓名,t_ageas年龄fromteacherwheret_name='{0}'", this.comboBox2.Text); } elseif(this.radioButton3.Checked) { sql=string.Format("selectidas学生编号,nameas学生姓名,sageas年龄fromstudentwhereid='{0}'", this.comboBox3.Text); } elseif(this.radioButton4.Checked) { sql=string.Format("selectidas学生编号,nameas学生姓名,sageas年龄fromstudentwherename='{0}'", this.comboBox4.Text); } DataTabletable=SqlManage.TableSelect(sql); if(table.Rows.Count>0) { this.dataGridView1.DataSource=table; } else { MessageBox.Show("没有相关内容"); } } privatevoidradioButton1_CheckedChanged(objectsender,EventArgse) { if(this.radioButton1.Checked) { this.comboBox1.Enabled=true; } else { this.comboBox1.Enabled=false; } } privatevoidradioButton2_CheckedChanged(objectsender,EventArgse) { if(this.radioButton2.Checked) { this.comboBox2.Enabled=true; } else { this.comboBox2.Enabled=false; } } privatevoidradioButton3_CheckedChanged(objectsender,EventArgse) { if(this.radioButton3.Checked) { this.comboBox3.Enabled=true; } else { this.comboBox3.Enabled=false; } } privatevoidradioButton4_CheckedChanged(objectsender,EventArgse) { if(this.radioButton4.Checked) { this.comboBox4.Enabled=true; } else { this.comboBox4.Enabled=false; } } } }
以上所述是小编给大家介绍的c#操作sqlserver2008的界面实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!