主界面

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public static int bs = 0;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
carDA da = new carDA();
dataGridView1.DataSource = da.select();
dataGridView1.ClearSelection();

brandDA bbb = new brandDA();
comboBox1.DataSource = bbb.Select();
comboBox1.DisplayMember = "Brand_name";
comboBox1.ValueMember = "Brand_code";

}

private void button2_Click(object sender, EventArgs e)
{
MessageBoxButtons btn = MessageBoxButtons.YesNoCancel;
if (MessageBox.Show("确定要删除么?", "删除数据", btn) == DialogResult.Yes)
{
car data = dataGridView1.SelectedRows[0].DataBoundItem as car;
carDA da = new carDA();
da.delete(data.Code);
dataGridView1.DataSource = da.select();
}
}

private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
//carDA da = new carDA();
car data = dataGridView1.SelectedRows[0].DataBoundItem as car;
xiugai xg = xiugai.NewXiuGai(data.Code);
xg.Show();
xg.Focus();
//dataGridView1.DataSource = da.select();
}
else
{
MessageBox.Show("没有选中任何项!");
}
}

private void button1_Click(object sender, EventArgs e)
{

tianjia tj = tianjia.Newtianjia();
tj.Show();
tj.Focus();

}

private void timer1_Tick(object sender, EventArgs e)
{
if (bs == 1)
{
carDA da = new carDA();
dataGridView1.DataSource = da.select();
bs = 0;
}
}

private void button4_Click(object sender, EventArgs e)
{

string name = textBox1.Text;
string brand=comboBox1.SelectedValue.ToString();

carDA da = new carDA();
dataGridView1.DataSource = da.Select(name,brand);
dataGridView1.AutoGenerateColumns = false;
}
}
}

连接类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
public class DBconnect
{
private static string connstring="server=.;database=mydb;user=sa;pwd=1023823348";
public static SqlConnection Conn
{
get
{
return new SqlConnection(connstring);
}
}
}
}

实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication2
{
public class car
{
private string code;

public string Code
{
get { return code; }
set { code = value; }
}
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string brand;

public string Brand
{
get { return brand; }
set { brand = value; }
}
private DateTime time;

public DateTime Time
{
get { return time; }
set { time = value; }
}
private decimal oil;

public decimal Oil
{
get { return oil; }
set { oil = value; }
}
private int powers;

public int Powers
{
get { return powers; }
set { powers = value; }
}
private int exhaust;

public int Exhaust
{
get { return exhaust; }
set { exhaust = value; }
}
private decimal price;

public decimal Price
{
get { return price; }
set { price = value; }
}
private string pic;

public string Pic
{
get { return pic; }
set { pic = value; }
}
}
}

操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
public class carDA
{
private SqlConnection _conn;
private SqlCommand _cmd;
private SqlDataReader _dr;

public carDA()
{
_conn = DBconnect.Conn;
_cmd = _conn.CreateCommand();
}
public List<car> select()
{
List<car> list = new List<car>();
_cmd.CommandText = "select * from car ";
_conn.Open();
_dr = _cmd.ExecuteReader();
if (_dr.HasRows)
{

while (_dr.Read())
{
car data = new car();
data.Code = _dr[0].ToString();
data.Name = _dr[1].ToString();
data.Brand = _dr[2].ToString();
data.Time = Convert.ToDateTime(_dr[3]);
data.Oil = Convert.ToDecimal(_dr[4]);
data.Powers = Convert.ToInt32(_dr[5]);
data.Exhaust = Convert.ToInt32(_dr[6]);
data.Price = Convert.ToDecimal(_dr[7]);
data.Pic = _dr[8].ToString();
list.Add(data);
}
}
_conn.Close();
return list;
}
public car select(string code)
{

_cmd.CommandText = "select * from car where Code=@code";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@code", code);
_conn.Open();

_dr = _cmd.ExecuteReader();
car data = new car();
if (_dr.HasRows)
{

_dr.Read();
data.Code = _dr[0].ToString();
data.Name = _dr[1].ToString();
data.Brand = _dr[2].ToString();
data.Time = Convert.ToDateTime(_dr[3]);
data.Oil = Convert.ToDecimal(_dr[4]);
data.Powers = Convert.ToInt32(_dr[5]);
data.Exhaust = Convert.ToInt32(_dr[6]);
data.Price = Convert.ToDecimal(_dr[7]);
data.Pic = _dr[8].ToString();

}
_conn.Close();
return data;
}
public void delete(string code)
{
_cmd.CommandText = "delete from car where Code=@code";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@code", code);
_conn.Open();
_cmd.ExecuteNonQuery();
_conn.Close();
}
public void update(string code, string name, string brand, DateTime time, decimal oil, int powers, int exhaust, decimal price, string pic)
{
_cmd.CommandText = "update car set Name=@name,Brand=@brand,Time=@time,Oil=@oil,Powers=@powers,Exhaust=@exhaust,Price=@price,Pic=@pic where Code = @code";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@code", code);
_cmd.Parameters.AddWithValue("@name", name);
_cmd.Parameters.AddWithValue("@brand", brand);
_cmd.Parameters.AddWithValue("@time", time);
_cmd.Parameters.AddWithValue("@oil", oil);
_cmd.Parameters.AddWithValue("@powers", powers);
_cmd.Parameters.AddWithValue("@exhaust", exhaust);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@pic", pic);
_conn.Open();
_cmd.ExecuteNonQuery();
_conn.Close();
}
public void insert(string code, string name, string brand, DateTime time, decimal oil, int powers, int exhaust, decimal price, string pic)
{
_cmd.CommandText = "insert into car values(@code, @name,@brand,@time,@oil,@powers,@exhaust,@price,@pic) ";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@code", code);
_cmd.Parameters.AddWithValue("@name", name);
_cmd.Parameters.AddWithValue("@brand", brand);
_cmd.Parameters.AddWithValue("@time", time);
_cmd.Parameters.AddWithValue("@oil", oil);
_cmd.Parameters.AddWithValue("@powers", powers);
_cmd.Parameters.AddWithValue("@exhaust", exhaust);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@pic", pic);
_conn.Open();
_cmd.ExecuteNonQuery();
_conn.Close();
}
public List<car> Select(string name, string brand)
{

string tj1 = " 1=1 ";
string tj2 = " 1=1 ";

if (name != "")
{
tj1 = " Name like @name ";
}

if (brand != "")
{
tj2 = " brand = @brand ";
}

string ztj = " where " + tj1 + " and " + tj2;

List<car> list = new List<car>();

_cmd.CommandText = "select * from car " + ztj;
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", "%" + name + "%");
_cmd.Parameters.AddWithValue("@brand", brand);

_conn.Open();
_dr = _cmd.ExecuteReader();
if (_dr.HasRows)
{

while (_dr.Read())
{
car data = new car();
data.Code = _dr[0].ToString();
data.Name = _dr[1].ToString();
data.Brand = _dr[2].ToString();
data.Time = Convert.ToDateTime(_dr[3]);
data.Oil = Convert.ToDecimal(_dr[4]);
data.Powers = Convert.ToInt32(_dr[5]);
data.Exhaust = Convert.ToInt32(_dr[6]);
data.Price = Convert.ToDecimal(_dr[7]);
data.Pic = _dr[8].ToString();
list.Add(data);
}

}
_conn.Close();

return list;

}

}
}

添加页面

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 WindowsFormsApplication2
{
public partial class tianjia : Form
{

private static tianjia tj = null;

public tianjia()
{
InitializeComponent();
}

private void tianjia_Load(object sender, EventArgs e)
{
carDA da = new carDA();

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
}
public static tianjia Newtianjia()
{
if (tj == null || tj.IsDisposed)
{
tj = new tianjia();
}

return tj;
}

private void button1_Click_1(object sender, EventArgs e)
{
string _code = textBox1.Text;
string _name = textBox2.Text;
string _brand = textBox3.Text;
DateTime _time = Convert.ToDateTime(textBox4.Text);
decimal _oil = Convert.ToDecimal(textBox5.Text);
int _powers = Convert.ToInt32(textBox6.Text);
int _exhaust = Convert.ToInt32(textBox7.Text);
decimal _price = Convert.ToDecimal(textBox8.Text);
string _pic = textBox9.Text;
carDA aaa = new carDA();
aaa.insert(_code, _name, _brand, _time, _oil, _powers, _exhaust, _price, _pic);
Form1.bs = 1;
this.Close();
}
}
}

修改页面

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 WindowsFormsApplication2
{
public partial class xiugai : Form
{
private string Code = "";

private static xiugai xg = null;

public xiugai()
{
InitializeComponent();
}
public xiugai(string code)
{
InitializeComponent();
this.Code = code;
}

private void xiugai_Load(object sender, EventArgs e)
{

carDA da = new carDA();
car data = da.select(Code);

textBox1.Text = data.Code;
textBox2.Text = data.Name;
textBox3.Text=data.Brand;
textBox4.Text = data.Time.ToString("yyyy-MM-dd HH:mm:ss");
textBox5.Text=data.Oil.ToString();
textBox6.Text=data.Powers.ToString();
textBox7.Text=data.Exhaust.ToString();
textBox8.Text=data.Price.ToString();
textBox9.Text = data.Pic;
}
public static xiugai NewXiuGai(string code)
{
if (xg == null || xg.IsDisposed)
{
xg = new xiugai(code);
}

return xg;
}

private void button1_Click(object sender, EventArgs e)
{
string _code = textBox1.Text;
string _name = textBox2.Text;
string _brand = textBox3.Text;
DateTime _time =Convert.ToDateTime(textBox4.Text);
decimal _oil = Convert.ToDecimal(textBox5.Text);
int _powers = Convert.ToInt32(textBox6.Text);
int _exhaust = Convert.ToInt32(textBox7.Text);
decimal _price = Convert.ToDecimal(textBox8.Text);
string _pic = textBox9.Text;
carDA aaa = new carDA();
aaa.update(_code, _name, _brand, _time, _oil, _powers, _exhaust, _price, _pic);
Form1.bs = 1;
this.Close();

}
}
}

brand系列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication2
{
public class brand
{
private string brand_code;

public string Brand_code
{
get { return brand_code; }
set { brand_code = value; }
}
private string brand_name;

public string Brand_name
{
get { return brand_name; }
set { brand_name = value; }
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
public class brandDA
{

private SqlConnection _conn;
private SqlCommand _cmd;
private SqlDataReader _dr;

public brandDA()
{
_conn = DBconnect.Conn;
_cmd = _conn.CreateCommand();
}
public List<brand> Select()
{
_cmd.CommandText = "select * from Brand";
_conn.Open();
_dr = _cmd.ExecuteReader();

List<brand> list = new List<brand>();

if (_dr.HasRows)
{
while (_dr.Read())
{
brand data = new brand();
data.Brand_code = _dr[0].ToString();
data.Brand_name= _dr[1].ToString();

list.Add(data);
}
}

_conn.Close();

return list;
}

public string BrandName(string code)
{
string name = "";
_cmd.CommandText = "select Brand_Name from Brand where Brand_Code=@code";
_cmd.Parameters.AddWithValue("@code", code);

_conn.Open();

_dr = _cmd.ExecuteReader();

if (_dr.HasRows)
{
_dr.Read();
name = _dr[0].ToString();
}

_conn.Close();
return name;
}
}
}

最新文章

  1. Spring源码分析——BeanFactory体系之抽象类、类分析(二)
  2. 北斗/GPS
  3. Unix操作系统中UUCP知识详细讲解
  4. 【NOIP2007】矩阵取数
  5. 8款耀眼的jQuery/HTML5焦点图滑块插件
  6. DTLS-PSK算法抓包解析
  7. Mybatis使用总结-思维导图
  8. Scikit-learn:主要模块和基本使用方法
  9. impala系列: 同步Hive元数据和收集统计信息
  10. IDEA修改git账号及密码的方法 ----绝壁好使
  11. Unity游戏开发常用的一些函数用法
  12. slf4j、jcl、jul、log4j1、log4j2、logback大总结
  13. PV&amp;UV&amp;IP之间的区别和联系
  14. LoadRunner 测试 Mysql
  15. 【Android】自己定义ListView的Adapter报空指针异常解决方法
  16. Java类型简介
  17. 微信Web开发者工具-下载、安装和使用图解
  18. [实战]MVC5+EF6+MySql企业网盘实战(7)——文件上传
  19. C和指针之学习笔记(5)
  20. Eclipse默认编码格式设置方式

热门文章

  1. 选择改变事件OnCheckedChange
  2. 可见性-volatile
  3. 解密所有APP运行过程中的内部逻辑(转)
  4. 【mybatis】mybatis 中select 查询 select * 查询出来的数据,字段值带不出来 数据不全
  5. webpack配置构建环境问题汇总
  6. Java 和 数据库两种方式进行加锁
  7. iOS:二维码的扫描
  8. Simple JavaScript Inheritance(John Resig)
  9. Yii2 使用十二 配合ajaxFileUpload 上传文件
  10. Kubernetes概念介绍和v1版本部署过程