winfrom查询/增加/修改/删除
一、查询
private void Form3_Load(object sender, EventArgs e) { //1.书写sql语句 string sql = "select *from PcInfo p join Brand b on p.id=b.id"; //声明一个DataTable的值去调用DBhelepr类,并传入参数 DataTable da = DBhelepr.DataTable(sql); //关闭自动生成列的属性 this.dataGridView1.AutoGenerateColumns = false; //绑定数据源,数据库的数据渲染到控件上 this.dataGridView1.DataSource = da;
模糊查询
private void button1_Click(object sender, EventArgs e) { //获取用户输入的内容 string pcName = this.textBox1.Text;//模糊查询文本框中获取到用户输入的内容 string sql = "select *from PcInfo p join Brand b on p.id=b.id where p.pcName like '%" + pcName+"%' ";//sql语句 这里是连表where p.pcName like '%" + pcName+"%' "才是核心 DataTable da = DBhelepr.DataTable(sql); this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.DataSource = da; }
选择查询
1.拉一个多选框控件
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 Forms { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //全局变量 //sql语句 static string sql = "select*, case HouseState when 1 then '在售' else '售罄' end as StateZN from Housing"; //数据集 static DataTable da = DBhelepr.DataTable(sql); private void Form1_Load(object sender, EventArgs e) { //关闭自动添加列的属性 this.dataGridView1.AutoGenerateColumns = false; //绑定数据源 this.dataGridView1.DataSource = da; //设置下拉列表项为第一个,默认显示第一项。将下标设置为0 this.comboBox1.SelectedIndex = 0; } //搜索单击事件 private void Button1_Click(object sender, EventArgs e) { //下拉光列表在售--1 拉光列表售空--2 int indexe = this.comboBox1.SelectedIndex; //如果下表是0,查询所有 否则按 if (indexe==0)//等于0重新绑定数据源加载 { //关闭自动添加列的属性 this.dataGridView1.AutoGenerateColumns = false; //绑定数据源 this.dataGridView1.DataSource = da; } //条件查询 else { string sql = "select*, case HouseState when 1 then '在售' else '售空' end as StateZN from Housing where HouseState="+indexe;//indexe是但在下拉框选择时会得到下标,会根据下标来查询对应的数据 DataTable da = DBhelepr.DataTable(sql); //关闭自动添加列的属性 this.dataGridView1.AutoGenerateColumns = false; //绑定数据源 this.dataGridView1.DataSource = da; } }
添加
在单选框选择男或女的方法
string HouseState = this.checkBox1.Checked ? "1" : "2";
控件名.Checked 后接三目运算符 意思是 等于1吗? 不等于就是等于2 这时候就会选择到是男或女的意思了
添加都是(1)获取用户输入的内容(2)书写sql语句(3)增删改是个布尔值类型的,所以声明一个bool来接受 bool facll = DBhelepr.ExecueNonQuerty(sql);(4)if判断,为真就是添加成功,反之!
删除
在查询的数据表中删除,就要获取当前选中的列然后执行删除
private void 删除书籍ToolStripMenuItem_Click(object sender, EventArgs e) { string id = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();//获得当前选中的列 是根据id这一行进行条件进行删除 string sql = "delete from tb_Book where BID=" + id; bool fain = DBhelep.ExecuteNonQuery(sql); DialogResult tr= MessageBox.Show("确定删除?","提示信息",MessageBoxButtons.OKCancel);//对弹窗进行选择, 会弹出确定或取消 这里就是 DialogResult 这个属性 if (tr==DialogResult.OK) { if (fain) { MessageBox.Show("删除成功", "提示信息"); Form1_Load(sender, e); } else { MessageBox.Show("删除失败", "提示信息"); } } }
修改
private void Button1_Click(object sender, EventArgs e) { //获取用户输入的信息 string BName = tbBName.Text; string BAuthor = tbBAuthor.Text; string BSort = tbBSort.Text; string BNum = tbBNum.Text; string BContent = textBox5.Text; //获取当前选中的ID string id = this.dataGridView1.CurrentRow.Cells[1].Value.ToString(); //sql语句 where条件进行修改 string sql = string.Format("update tb_Book set BName='{0}',BNum='{1}',BSort='{2}',BContent='{3}',BAuthor='{4}' where BID={5}",BName,BNum,BSort,BContent, BAuthor,id); bool fain = DBhelep.ExecuteNonQuery(sql); if (fain) { MessageBox.Show("修改成功","提示信息"); } else { MessageBox.Show("修改失败", "提示信息"); } }
把数据表数据渲染到文本显示框中
是当点击数据表中的某一个数据时,显示文本框就会得到相对应的数值 所以是个单击事件
private void DataGridView1_Click(object sender, EventArgs e) { //语法: this.显示的文本控件名.text=数据表名称.CurrentRow.Cells[对应数据的下标,在编辑数据表里找]Value.ToString(); //=得到数据表名.进入该表查询.得到一个对应的下标.得到下标对应的值.ToString(); this.tbBName.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); this.tbBAuthor.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString(); this.tbBSort.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString(); this.tbBNum.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString(); this.textBox5.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString(); }

浙公网安备 33010602011771号