
先是查询整张表,用到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}'',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) { 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_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 学生编号,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 的界面实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的c# *** 作sql server2008 的界面实例代码全部内容,希望文章能够帮你解决c# *** 作sql server2008 的界面实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)