UI层
private void btnAddRack_Click(object sender, EventArgs e)
{
FrmBookrackInfo frmBookrackInfo = new FrmBookrackInfo();
frmBookrackInfo.BookrackInfoEventHandler += new Action<Model.BookrackInfo>(frmBookrackInfo_BookrackEventHandler);
if (frmBookrackInfo.ShowDialog()==DialogResult.OK)
{
dgvBookreckInfo.DataSource = bllBookrackInfo.GetReckInfo();
}
frmBookrackInfo.Dispose();
}
private void frmBookrackInfo_BookrackEventHandler(Model.BookrackInfo obj)
{
bllBookrackInfo.InsertData(obj);
}
private void btnChangeRack_Click(object sender, EventArgs e)
{
Model.BookrackInfo bookrackInfoOld = dgvBookreckInfo.SelectedRows[0].DataBoundItem as Model.BookrackInfo;
if (bookrackInfoOld==null)
{
MessageBox.Show("请选中一行");
return;
}
//显示修改界面
FrmBookrackInfo frmBookrackInfo = new FrmBookrackInfo(bookrackInfoOld);
//绑定事件
frmBookrackInfo.BookrackInfoEventHandler += new Action<Model.BookrackInfo>(frmBookrackInfo_BookRackInfoEventHandler);
if (frmBookrackInfo.ShowDialog()==DialogResult.OK)
{
dgvBookreckInfo.DataSource = bllBookrackInfo.GetReckInfo();
}
}
void frmBookrackInfo_BookRackInfoEventHandler(Model.BookrackInfo obj)
{
bllBookrackInfo.Update(obj);
}
private void btnDeleterack_Click(object sender, EventArgs e)
{
if (dgvBookreckInfo.SelectedRows.Count==0)
{
MessageBox.Show("请选中要删除的行"); return;
}
List<int> listId = new List<int>();
for (int i = 0; i < dgvBookreckInfo.SelectedRows.Count; i++)
{
Model.BookrackInfo brk = dgvBookreckInfo.SelectedRows[i].DataBoundItem as Model.BookrackInfo;
if (brk == null) continue;
listId.Add(brk.ID);
}
if (bllBookrackInfo.DeleteByIDs(listId))
{
MessageBox.Show("删除成功");
dgvBookreckInfo.DataSource = bllBookrackInfo.GetReckInfo();
}
else
{
MessageBox.Show("删除失败...");
}
}
comm类
public class Commom
{
public static bool CheckIsNullOrEmpty(params string[] args)
{
bool isNullOrEmpty = false;
for (int i = 0; i < args.Length; i++)
{
if (string.IsNullOrEmpty(args[i]))
{
isNullOrEmpty = true;
break;
}
}
return isNullOrEmpty;
}
操作页面
public partial class FrmBookrackInfo : Form
{
public FrmBookrackInfo()
{
InitializeComponent();
this.btnAccept.Text = "添加";
cboBookRegionId.SelectedIndex = -1;
}
public event Action<Model.BookrackInfo> BookrackInfoEventHandler;
BLL.BookrackInfo bllBookrackInf = new BLL.BookrackInfo();
BLL.BookRegionInfo bllBookRegionInfo = new BLL.BookRegionInfo();
Model.BookrackInfo bookrackInfoOld;
public FrmBookrackInfo(Model.BookrackInfo bookrackInfo)
: this()
{
this.bookrackInfoOld = bookrackInfo;
this.btnAccept.Text = "修改";
}
private void btnAccept_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i] is TextBox)
{
if (Commom.CheckIsNullOrEmpty(this.Controls[i].Text))
{
MessageBox.Show("请输入完整信息");
return;
}
}
}
Model.BookrackInfo bookrackInfo = new Model.BookrackInfo();
bookrackInfo.BookrackId = Convert.ToInt32(txtBookrackId.Text.Trim());
bookrackInfo.BookrackNum = Convert.ToInt32(txtBookrackNum.Text.Trim());
bookrackInfo.Remark = txtRemark.Text.Trim();
bookrackInfo.BookRegionId = (cboBookRegionId.SelectedItem as
Model.BookRegionInfo) == null ? 0 : (cboBookRegionId.SelectedItem as Model.BookRegionInfo).ID;
if (bookrackInfoOld == null)
{
//新增
AddBookrackInfo(bookrackInfo);
}
else
{
//修改
if (!bookrackInfo.Equals(bookrackInfoOld))
{
bookrackInfo.ID = bookrackInfoOld.ID;
//不同进行修改
ChangeBookrackInfo(bookrackInfo);
;
}
}
this.DialogResult = DialogResult.OK;
}
private void ChangeBookrackInfo(Model.BookrackInfo bookrackInfo)
{
if (bookrackInfo.Equals(bookrackInfoOld))
{
MessageBox.Show("相同不用修改");
return;
}
else if (BookrackInfoEventHandler != null)
{
BookrackInfoEventHandler(bookrackInfo);
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
}
//比较用户修改的对象与从数据库中取出的对象的关系,如果有相同的字段就将其设置为null或-1,那么后台的执行逻辑就不为这个字段生成SQL语句的字段
private void ChangbookrackInfoToChange(Model.BookrackInfo bookrackInfo, Model.BookrackInfo bookrackInfoOld)
{
//凡是相同的就为null,只保留需要修改的字段
if (bookrackInfo.ID == bookrackInfoOld.ID)
{
bookrackInfo.ID = -1;
}
if (bookrackInfo.BookrackId == bookrackInfoOld.BookrackId)
{
bookrackInfo.BookrackId = -1;
}
if (bookrackInfo.BookRegionId == bookrackInfoOld.BookRegionId)
{
bookrackInfo.BookRegionId = -1;
}
if (bookrackInfo.BookrackNum == bookrackInfoOld.BookrackNum)
{
bookrackInfo.BookrackNum = -1;
}
if (bookrackInfo.Remark == bookrackInfoOld.Remark)
{
bookrackInfo.Remark = null;
}
}
private void AddBookrackInfo(Model.BookrackInfo bookrackInfo)
{
if (BookrackInfoEventHandler != null)
{
BookrackInfoEventHandler(bookrackInfo);
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
}
private void FrmBookrackInfo_Load(object sender, EventArgs e)
{
if (bookrackInfoOld == null)
{
LoadDataComboBoxAdd();
}
else
{
LoadDataComboBoxChange();
}
}
private void LoadDataComboBoxAdd()
{
List<Model.BookRegionInfo> list = bllBookRegionInfo.GetRegionInfo();
//cboBookRegionId.DataSource = list;
//cboBookRegionId.DisplayMember = "RegionName";
//cboBookRegionId.SelectedIndex = 0;
cboBookRegionId.Items.Add("请选择");
cboBookRegionId.DisplayMember = "RegionName";
for (int i = 0; i < list.Count; i++)
{
cboBookRegionId.Items.Add(list[i]);
}
cboBookRegionId.SelectedIndex = 0;
}
private void LoadDataComboBoxChange()
{
LoadDataComboBoxAdd();
for (int i = 0; i < cboBookRegionId.Items.Count; i++)
{
Model.BookRegionInfo bookRegionInfo = cboBookRegionId.Items[i] as Model.BookRegionInfo;
if (bookRegionInfo == null) continue;
if (bookRegionInfo.ID == bookrackInfoOld.BookRegionId)
{
cboBookRegionId.SelectedIndex = i;
break;
}
}
txtBookrackId.Text = bookrackInfoOld.BookrackId.ToString();
txtBookrackNum.Text = bookrackInfoOld.BookrackNum.ToString();
txtRemark.Text = bookrackInfoOld.Remark;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.No;
}
private void txtBookrackNum_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '1' || e.KeyChar > '9')
{
e.Handled = true;
return;
}
}
}
}
BLL层
namespace BookManage.BLL
{
public class BookrackInfo
{
DAL.BookRackInfo dalbookRack = new DAL.BookRackInfo();
public List<Model.BookrackInfo> GetReckInfo()
{
return dalbookRack.GetReckInfo();
}
public bool DeleteByIDs(List<int> listId)
{
return dalbookRack.DeleteByIDs(listId)>0;
}
public void InsertData(Model.BookrackInfo obj)
{
dalbookRack.InsertData(obj);
}
public void Update(Model.BookrackInfo obj)
{
dalbookRack.Update(obj);
}
}
}
DAL层
public class BookRackInfo
{
Model.BookrackInfo bookrack = new Model.BookrackInfo();
public List<Model.BookrackInfo> GetReckInfo()
{
List<Model.BookrackInfo> list = new List<Model.BookrackInfo>();
string sql = "select ID,BookrackId,BookRegionId,BookrackNum,Remark from BookrackInfo";
using (SqlDataReader reader=DALSqlHelper.SqlHelper.ExecuteReader(sql,CommandType.Text))
{
// list.Add(Comm.GetInfoFromReader<Model.BookrackInfo>(reader));
while (reader.Read())
{
list.Add(Comm.GetBookrackInfoReader(reader));
}
}
return list;
}
public int DeleteByIDs(List<int> listId)
{
string sql =string.Format( "delete from BookRackInfo where ID in({0}) ",string.Join(",",listId));
return SqlHelper.ExecuteNonQuery(sql,CommandType.Text);
}
public void InsertData(Model.BookrackInfo obj)
{
List<Model.BookrackInfo> list = new List<Model.BookrackInfo>();
string sql = "insert into BookrackInfo (BookrackId,BookRegionId,BookrackNum,Remark)values(@BookrackId,@BookRegionId,@BookrackNum,@Remark)";
SqlParameter[] ps = {
new SqlParameter("@BookrackId",obj.BookrackId),
new SqlParameter("@BookRegionId",obj.BookRegionId),
new SqlParameter("@BookrackNum",obj.BookrackNum),
new SqlParameter("@Remark",obj.Remark)
};
SqlHelper.ExecuteNonQuery(sql,CommandType.Text,ps);
}
public void Update(Model.BookrackInfo obj)
{
//List<Model.BookrackInfo> list = new List<Model.BookrackInfo>();
string sql = "update BookrackInfo set BookrackId=@BookrackId,BookrackNum=@BookrackNum,BookRegionId=@BookRegionId,Remark=@Remark where ID=@ID";
SqlParameter[] ps = {
new SqlParameter("@BookrackId",obj.BookrackId),
new SqlParameter("@BookrackNum",obj.BookrackNum),
new SqlParameter("@BookRegionId",obj.BookRegionId),
new SqlParameter("@Remark",obj.Remark),
new SqlParameter("@ID",obj.ID)
};
//List<string> paraName = new List<string>();
//List<SqlParameter> para = new List<SqlParameter>();
//if (obj.BookrackId!=0)
//{
// paraName.Add("BookrackId=@BookrackId");
// para.Add(new SqlParameter("@BookrackId",obj.BookrackId));
//}
//if (obj.BookrackNum!=0)
//{
// paraName.Add("BookrackNum=@BookrackNum");
// para.Add(new SqlParameter("@BookrackNum",obj.BookrackNum));
//}
//if (obj.BookRegionId!=0)
//{
// paraName.Add("BookRegionId=@BookRegionId");
// para.Add(new SqlParameter("@BookRegionId",obj.BookRegionId));
//}
//if (obj.Remark!=null)
//{
// paraName.Add("Remark=@Remark");
// para.Add(new SqlParameter("@Remark",obj.Remark));
//}
//if (obj.ID != 0)
//{
// paraName.Add("ID=@ID");
// para.Add(new SqlParameter("@ID", obj.ID));
//}
SqlHelper.ExecuteNonQuery(sql,CommandType.Text,ps);
//string sql = "update BookrackInfo set "+string.Join(",",paraName);
//SqlHelper.ExecuteNonQuery(sql,CommandType.Text,para.ToArray());
}
public int GetRackCountByUsing(List<int> regionIdlist)
{
string sql = "select count(*) from BookrackInfo where BookRegionId in( "+string.Join(",",regionIdlist)+")";
return (int)SqlHelper.ExecuteScalar(sql,CommandType.Text);
}
public void DeleteByrackIDs(List<int> regionIdlist)
{
string sql = string.Format("delete from BookrackInfo where BookrackId in({0})",string.Join(",",regionIdlist));
SqlHelper.ExecuteNonQuery(sql,CommandType.Text);
}
}
}
commcon层
public class Comm
{
public static T GetInfoFromReader<T>(SqlDataReader reader)
{
Type t=typeof(T);
object o = Activator.CreateInstance(t);
for (int i = 0; i < reader.FieldCount; i++)
{
string name = reader.GetName(i);
PropertyInfo p = t.GetProperty(name,BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public);
p.SetValue(o,reader[i],null);
}
return (T)o;
}
public static Model.BookRegionInfo GetBookRegionInfoReader(SqlDataReader reader)
{
Model.BookRegionInfo bookRegion = new Model.BookRegionInfo();
bookRegion.ID = Convert.ToInt32(reader["ID"]);
bookRegion.RegionName = reader["RegionName"].ToString();
bookRegion.Remark = reader["Remark"].ToString();
return bookRegion;
}
public static Model.BookrackInfo GetBookrackInfoReader(SqlDataReader reader)
{
Model.BookrackInfo bookrackInfo = new Model.BookrackInfo();
bookrackInfo.ID = Convert.ToInt32(reader["ID"]);
bookrackInfo.BookrackId = Convert.ToInt32(reader["BookrackId"]);
bookrackInfo.BookRegionId = Convert.ToInt32(reader["BookRegionId"]);
bookrackInfo.BookrackNum = Convert.ToInt32(reader["BookrackNum"]);
bookrackInfo.Remark = reader.IsDBNull(4) ? null:reader["Remark"].ToString();
return bookrackInfo;
}
}
}
Model层
public class BookrackInfo
{
public int ID { get; set; }
public int BookrackId { get; set; }
public int BookRegionId { get; set; }
public int BookrackNum { get; set; }
public string Remark { get; set; }
public override bool Equals(object obj)
{
Model.BookrackInfo bookrackInfo = obj as Model.BookrackInfo;
return this.BookrackId == bookrackInfo.BookrackId &&
this.BookRegionId == bookrackInfo.BookRegionId &&
this.BookrackNum == bookrackInfo.BookrackNum &&
this.Remark == bookrackInfo.Remark &&
this.ID == bookrackInfo.ID;
}
public override string ToString()
{
return BookRegionId.ToString();
}
}