这是一个简单的手机查询系统
查所有
按查询手机类型查询
按品牌进行查询
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;
using System.Data.SqlClient;
namespace FrmMobileManager
{
public partial class Form1 : Form
{
DBHelper helper = new DBHelper();
DataSet ds = null;
DataView dv = null;
SqlDataAdapter adapter = null;
public Form1()
{
InitializeComponent();
}
private void clear_Click(object sender, EventArgs e)
{
this.cboTypes.Text ="全部";
this.txtQuery.Clear();
}
private void close_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定退出吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result==DialogResult.Yes)
{
Application.Exit();
}
}
private void select_Click(object sender, EventArgs e)
{
if (Band()==true)
{
if (this.cboTypes.Text.Trim().Equals("全部")&&this.txtQuery.Text.Trim()==string.Empty)
{
Form1_Load(sender,e);
}
if (this.cboTypes.Text.Trim().Equals("手机类型")&&this.txtQuery.Text.Trim()!=null)
{
SelectCategory();
}
if (this.cboTypes.Text.Trim().Equals("品牌")&& this.txtQuery.Text.Trim()!=null)
{
SelectType();
}
}
}
private void SelectType()
{
try
{
string sql = string.Format(@"SELECT [Mid]as '序号'
,[Bland]as '品牌'
,[Type]as '型号'
,[price]as '价格'
,CategoryInfo.Category as '手机类型'
FROM [MobileInfo]inner join CategoryInfo on MobileInfo.Cid=CategoryInfo.Cid
where Bland='{0}'", this.txtQuery.Text.Trim());
adapter = new SqlDataAdapter(sql, helper.Connection);
ds = new DataSet();
if (ds.Tables["MobileInfo"] != null)
{
ds.Tables["MobileInfo"].Clear();
}
adapter.Fill(ds, "MobileInfo");
dv = new DataView(ds.Tables["MobileInfo"]);
this.dgvSelect.DataSource = dv;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SelectCategory()
{
string sql = string.Format(@"SELECT [Mid]as '序号'
,[Bland]as '品牌'
,[Type]as '型号'
,[price]as '价格'
,CategoryInfo.Category as '手机类型'
FROM [MobileInfo]inner join CategoryInfo on MobileInfo.Cid=CategoryInfo.Cid
where CategoryInfo.Category='{0}'",this.txtQuery.Text.Trim());
adapter = new SqlDataAdapter(sql, helper.Connection);
ds = new DataSet();
if (ds.Tables["MobileInfo"] != null)
{
ds.Tables["MobileInfo"].Clear();
}
adapter.Fill(ds, "MobileInfo");
dv = new DataView(ds.Tables["MobileInfo"]);
this.dgvSelect.DataSource = dv;
}
private bool Band()
{
if (this.cboTypes.Text.Trim()==string.Empty&&this.txtQuery.Text.Trim()==string.Empty)
{
MessageBox.Show("查询条件和查询种类都不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
return true;
}
private void Form1_Load(object sender, EventArgs e)
{
dgvSelect1();
}
private void dgvSelect1()
{
string sql = string.Format(@"SELECT [Mid]as '序号'
,[Bland]as '品牌'
,[Type]as '型号'
,[price]as '价格'
,CategoryInfo.Category as '手机类型'
FROM [MobileInfo],CategoryInfo
where MobileInfo.Cid=CategoryInfo.Cid");
adapter = new SqlDataAdapter(sql, helper.Connection);
ds = new DataSet();
if (ds.Tables["MobileInfo"] != null)
{
ds.Tables["MobileInfo"].Clear();
}
adapter.Fill(ds, "MobileInfo");
dv = new DataView(ds.Tables["MobileInfo"]);
this.dgvSelect.DataSource = dv;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace FrmMobileManager
{
class DBHelper
{
//数据库四参数
private string strConn = "Data Source=.;Initial Catalog=MobileManager;User ID=sa;Pwd=1234";
//Connection对象
private SqlConnection connection;
public SqlConnection Connection
{
get
{
if (connection==null)
{
connection=new SqlConnection(strConn);
}
return connection;
}
}
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
}

3664

被折叠的 条评论
为什么被折叠?



