数据访问类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Interview
{
class OperatorConnection
{
SqlConnection objSqlConnection = null;
SqlCommand objSqlCommand = null;
DataSet objDataSet = null;
SqlDataAdapter objSqlDataAdapter = null;
public OperatorConnection()
{
objSqlConnection = new SqlConnection("database=Wear;server=.;uid=sa;pwd=sa");
objSqlConnection.Open();
if (objSqlCommand == null)
{
objSqlCommand = new SqlCommand();
objSqlCommand.Connection = this.objSqlConnection;
}
}
public DataSet search(String sql, String strTableName)
{
try
{
objDataSet = new DataSet();
objSqlCommand.CommandType = CommandType.Text;
objSqlCommand.CommandText = sql;
objSqlDataAdapter = new SqlDataAdapter();
objSqlDataAdapter.SelectCommand = objSqlCommand;
objSqlDataAdapter.Fill(objDataSet,strTableName);
}
catch (Exception ce)
{
throw ce;
}
return objDataSet;
}
}
}
业务层:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Interview
{
class common
{
DataSet objDataSet = null;
public DataSet findGoods(String ID,DateTime startTime,DateTime endTime)
{
OperatorConnection objConnection = new OperatorConnection();
objDataSet = objConnection.search("select * from Sell_Details as se left join stock_Details as st on se.ClothesID = st.ClothesID left join ClothesMessage as cl on st.ProduceID = cl.ProducerID where st.date>'" + startTime + "' and st.date <'" + endTime + "'","Sell_Details");
// and SellID= '" + ID + "'"
return objDataSet;
}
public DataSet findListGoods(String ID)
{
OperatorConnection objConnection = new OperatorConnection();
objDataSet = objConnection.search("select * from Sell_Details where SellID = '" + ID + "'", "Sell_Details");
return objDataSet;
}
}
}
表示层:
public partial class frmGoods : Form
{
DataSet objDataSet = null;
public static String Number = null;
public frmGoods()
{
InitializeComponent();
;
}
private void Form1_Load(object sender, EventArgs e)
{
}
//查询的按钮
private void BtnSelect_Click(object sender, EventArgs e)
{
common objcommon = new common();
String strID = this.txtID.Text.ToString();
DateTime startTime = System.DateTime.Parse(this.dtpStartTime.Text.ToString());
DateTime endTime = System.DateTime.Parse(this.dtpEndTime.Text.ToString());
//隐藏主键 yourDS.Tables[yourTableName].Columns[yourKeyCol].ColumnMapping = MappingType.Hidden;
//this.dgv.DataSource = objDataSet;
objDataSet = objcommon.findGoods(strID, startTime, endTime);
//dataGrideView绑定数据 与dataGrid的区别是:它是一个视图,所以速度快
//DataGridView。与 DataGrid 不同的是,DataGridView 适用于各种不同的现实情况
//——无论是要处理数据绑定和用户编辑,还是仅涉及静态文本显示,都可以采用 DataGridView。
this.dgv.DataSource = this.objDataSet.Tables[0].DefaultView;
this.dgr.SetDataBinding(objDataSet, "Sell_Details");
}
//当前行变化 的事件
private void dgr_CurrentCellChanged(object sender, EventArgs e)
{
dgr.Select(dgr.CurrentCell.RowNumber);
Number = objDataSet.Tables[0].Rows[dgr.CurrentCell.RowNumber]["SellID"].ToString();
}
//
private void dgr_MouseDoubleClick(object sender, MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dgr.HitTest(pt);
if (hti.Type == DataGrid.HitTestType.Cell)
{
dgr.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dgr.Select(hti.Row);
}
//调用详细列表的窗体
frmListGoods objfrmListGoods = new frmListGoods();
objfrmListGoods.Show();
}
//
private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (this.dgv.Rows.Count != 0)
{
for (int i = 0; i < this.dgv.Rows.Count; )
{
this.dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink;
i += 2;
}
}
}
//dataGridView离开时的事件
private void dgv_MouseUp(object sender, MouseEventArgs e)
{
string value = this.dgv.Rows[dgv.CurrentRow.Index].Cells[0].Value.ToString();
MessageBox.Show(value);
}
}
public partial class frmListGoods : Form
{
DataSet objDataSet = null;
public frmListGoods()
{
InitializeComponent();
}
private void frmListGoods_Load(object sender, EventArgs e)
{
common objcommon = new common();
objDataSet = objcommon.findListGoods(frmGoods.Number);
this.dgrList.SetDataBinding(objDataSet, "Sell_Details");
//绑定文本框
DataView dv = this.objDataSet.Tables[0].DefaultView;
this.dgrList.DataSource = dv;
this.textBox1.DataBindings.Add(new Binding
("Text", dv, "DpotName"));
this.textBox2.DataBindings.Add(new Binding
("Text", dv, "CustomerID"));
}
}