目录
题目:
效果图:
数据库:
做法:
添加界面
datagirdview设置
全部代码:
DBHelper类
From1主窗体代码
添加代码:
该题最难的部分是查询学生按钮的四种的情况判断处理 ,在这里我用的方法是!与&&进行搭配使用,用if写出了四种情况,分别的查询语句当然也不一样
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text==""&&comboBox1.Text=="")
{
string s = "select * from tb_stu";
DataSet d = DBHelper.ds(s);
this.dataGridView1.DataSource = d.Tables[0];
}
else if (!(textBox2.Text == "") && !(comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where SName like '%{0}%' and State='{1}'", textBox2.Text, comboBox1.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
else if (!(textBox2.Text == "") && (comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where SName like '%{0}%' ", textBox2.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
else if ((textBox2.Text == "") && !(comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where State='{0}'",comboBox1.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
}
该页面主要代码:
private void button2_Click(object sender, EventArgs e) { if (textBox1.Text=="") { MessageBox.Show("姓名不能为空!"); } if (textBox3.Text == "") { MessageBox.Show("电话不能为空!"); } if (comboBox1.Text == "") { MessageBox.Show("类别不能为空!"); } string sql = string.Format("insert tb_stu(SName,SAddress,STel,Class,State)values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,comboBox1.Text); if (DBHelper.noqe(sql)) { MessageBox.Show("添加成功!"); } }
首先设置datagridview的这三个属性
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xuejixinxiguanli
{
internal class DBHelper
{
public static SqlConnection conn = null;
public static string connstr = "server=.;database=GCStudentDB;uid=sa;pwd=123456";
public static void into()
{
if (conn == null)
{
conn = new SqlConnection(connstr);
}
conn.Close();
conn.Open();
}
public static bool noqe(string sql)
{
into();
SqlCommand cmd = new SqlCommand(sql, conn);
int ret = cmd.ExecuteNonQuery();
conn.Close();
if (ret > 0)
{
return true;
}
else
{
return false;
}
}
public static DataSet ds(string sql)
{
into();
DataSet d = new DataSet();
SqlDataAdapter t = new SqlDataAdapter(sql, conn);
t.Fill(d);
conn.Close();
return d;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xuejixinxiguanli
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
jia j=new jia();
j.ShowDialog();
string sql = "select * from tb_stu";
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
string sql = "select * from tb_stu";
DataSet ds= DBHelper.ds(sql);
this.dataGridView1.DataSource= ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text==""&&comboBox1.Text=="")
{
string s = "select * from tb_stu";
DataSet d = DBHelper.ds(s);
this.dataGridView1.DataSource = d.Tables[0];
}
else if (!(textBox2.Text == "") && !(comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where SName like '%{0}%' and State='{1}'", textBox2.Text, comboBox1.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
else if (!(textBox2.Text == "") && (comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where SName like '%{0}%' ", textBox2.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
else if ((textBox2.Text == "") && !(comboBox1.Text == ""))
{
string sql = string.Format("select * from tb_stu where State='{0}'",comboBox1.Text);
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xuejixinxiguanli
{
public partial class jia : Form
{
public jia()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text=="")
{
MessageBox.Show("姓名不能为空!");
}
if (textBox3.Text == "")
{
MessageBox.Show("电话不能为空!");
}
if (comboBox1.Text == "")
{
MessageBox.Show("类别不能为空!");
}
string sql = string.Format("insert tb_stu(SName,SAddress,STel,Class,State)values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,comboBox1.Text);
if (DBHelper.noqe(sql))
{
MessageBox.Show("添加成功!");
}
}
}
}