BindingSource
BindingSource更新时更新表一定要有主键。Google了些资料,比较沉闷。
错误提示信息:对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成。
BindingSource的使用貌似并不影响传说中的分层。。。
代码
private void button7_Click(object sender, EventArgs e)
{
MonitorRecordBLL bll = new MonitorRecordBLL();
//更新表一定要有主键
DataTable dt = bll.UpdateByBindingSource((DataTable)bindingSource1.DataSource);
}
//BLL
public DataTable UpdateByBindingSource(DataTable dt)
{
MonitorRecordDAL dal = new MonitorRecordDAL();
DataTable datatable= dal.UpdateByBindingSource(dt);
return datatable;
}
//DAL
public DataTable UpdateByBindingSource(DataTable dt)
{
SqlProcess sp = new SqlProcess();
string strsql="select * from tddygx";
DataTable datatable=sp.UpdateByBindingSource(dt, strsql);
return datatable;
}
{
MonitorRecordBLL bll = new MonitorRecordBLL();
//更新表一定要有主键
DataTable dt = bll.UpdateByBindingSource((DataTable)bindingSource1.DataSource);
}
//BLL
public DataTable UpdateByBindingSource(DataTable dt)
{
MonitorRecordDAL dal = new MonitorRecordDAL();
DataTable datatable= dal.UpdateByBindingSource(dt);
return datatable;
}
//DAL
public DataTable UpdateByBindingSource(DataTable dt)
{
SqlProcess sp = new SqlProcess();
string strsql="select * from tddygx";
DataTable datatable=sp.UpdateByBindingSource(dt, strsql);
return datatable;
}
SqlHelper
代码
public DataTable UpdateByBindingSource(DataTable db,string strsql)
{
SqlConnection conn = new SqlConnection(SQL_CONNECTION_STRING);
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand(strsql, (SqlConnection)conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
try
{
lock (this) //处理并发情况(分布式情况)
{
myAdapter.Update(db);
}
}
catch (Exception)
{
conn.Close();
}
return db; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
//以上纯属摘用
{
SqlConnection conn = new SqlConnection(SQL_CONNECTION_STRING);
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand(strsql, (SqlConnection)conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
try
{
lock (this) //处理并发情况(分布式情况)
{
myAdapter.Update(db);
}
}
catch (Exception)
{
conn.Close();
}
return db; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
//以上纯属摘用