目录
题目:
效果图:
数据库:
做法:
添加界面
查询按钮功能
datagirdview设置
全部代码:
DBHelper类
From1主窗体代码
添加代码:
查询代码
信息管理下拉框中有两个选项,每一个都会打开对应的窗体
该页面主要代码:
该代码作用:
1 首先为Brand表添加brand数据,
2 红色字体是全新写法,查询别的表的内容
string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text);
private void button1_Click(object sender, EventArgs e) { try { if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && textBox4.Text != null && comboBox1.Text != null) { string sql = string.Format("insert Brand(brand)values ('{0}')", comboBox1.Text.ToString()); if (!DBHelper.noqe(sql)) { MessageBox.Show("品牌添加失败"); } byte temp = 0; if (radioButton2.Checked == true) { temp =1; } string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text); if (DBHelper.noqe(s)) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } } else { MessageBox.Show("请录入完整商品信息!"); } } catch (Exception ee) { MessageBox.Show(ee.ToString()); } }
需要用到连接查询,因为有两个表。再用模糊查询查询两个表的内容
private void button1_Click(object sender, EventArgs e)
{
string sql =string.Format("select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id where pcName like'%{0}%'",textBox1.Text) ;
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
首先设置datagridview的这三个属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace spxxgl
{
class DBHelper
{
public static SqlConnection conn = null;
public static string connstr = "server=.;database=GoodsManager;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 spxxgl
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 增加信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
jia j = new jia();
j.ShowDialog();
j.MdiParent = this;
}
private void 查询信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
cha j = new cha();
j.ShowDialog();
j.MdiParent = this;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
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 spxxgl
{
public partial class jia : Form
{
public jia()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && textBox4.Text != null && comboBox1.Text != null)
{
string sql = string.Format("insert Brand(brand)values ('{0}')", comboBox1.Text.ToString());
if (!DBHelper.noqe(sql))
{
MessageBox.Show("品牌添加失败");
}
byte temp = 0;
if (radioButton2.Checked == true)
{
temp =1;
}
string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text);
if (DBHelper.noqe(s))
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!");
}
}
else
{
MessageBox.Show("请录入完整商品信息!");
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = null;
comboBox1.Text = null;
radioButton1.Checked = true;
textBox4.Text = null;
textBox2.Text = null;
textBox3.Text = null;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void jia_Load(object sender, EventArgs e)
{
}
}
}
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 spxxgl
{
public partial class cha : Form
{
public cha()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sql =string.Format("select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id where pcName like'%{0}%'",textBox1.Text) ;
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void cha_Load(object sender, EventArgs e)
{
string sql = "select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id";
DataSet ds= DBHelper.ds(sql);
this.dataGridView1.DataSource= ds.Tables[0];
}
}
}