首页上一页 1 下一页尾页 1 条记录 1/1页
2011092803
发表在C#图书答疑
2011-09-28
是否精华
是
否
版块置顶:
是
否
c#案例开发全程实录第2版企业人事管理项目mymoudle.cs中保存添加和修改模块
#region 保存添加或修改的信息
/// <summary>
/// 保存添加或修改的信息.
/// </summary>
/// <param name="Sarr">数据表中的所有字段</param>
/// <param name="ID1">第一个字段值</param>
/// <param name="ID2">第二个字段值</param>
/// <param name="Contr">指定控件的数据集</param>
/// <param name="BoxName">要搜索的控件名称</param>
/// <param name="TableName">数据表名称</param>
/// <param name="n">控件的个数</param>
/// <param name="m">标识,用于判断是添加还是修改</param>
public void Part_SaveClass(string Sarr, string ID1, string ID2, Control.ControlCollection Contr, string BoxName, string TableName, int n, int m)
{
string tem_Field = "", tem_Value = "";
int p = 2;
if (m == 1){ //当m为1时,表示添加数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Field = "ID";
tem_Value = "'" + ID1 + "'";
p = 1;
}
else{
tem_Field = "Sut_id,ID";
tem_Value = "'" + ID1 + "','" + ID2 + "'";
}
}
else
if (m == 2){ //当m为2时,表示修改数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Value = "ID='" + ID1 + "'";
p = 1;
}
else
tem_Value = "Sut_ID='" + ID1 + "',ID='" + ID2 + "'";
}
if (m > 0){ //生成部份添加、修改语句
string[] Parr = Sarr.Split(Convert.ToChar(','));
for (int i = p; i < n; i++)
{
string sID = BoxName + i.ToString(); //通过BoxName参数获取要进行操作的控件名称
foreach (Control C in Contr){ //遍历控件集中的相关控件
if (C.GetType().Name == "TextBox" | C.GetType().Name == "MaskedTextBox" | C.GetType().Name == "ComboBox")
if (C.Name == sID){ //如果在控件集中找到相应的组件
string Ctext = C.Text;
if (C.GetType().Name == "MaskedTextBox") //如果当前是MaskedTextBox控件
Ctext = Date_Format(C.Text); //对当前控件的值进行格式化
if (m == 1){ //组合SQL语句中insert的相关语句
tem_Field = tem_Field + "," + Parr[i];
if (Ctext == "")
tem_Value = tem_Value + "," + "NULL";
else
tem_Value = tem_Value + "," + "'" + Ctext + "'";
}
if (m == 2)
{ //组合SQL语句中update的相关语句
if (Ctext=="")
tem_Value = tem_Value + "," + Parr[i] + "=NULL";
else
tem_Value = tem_Value + "," + Parr[i] + "='" + Ctext + "'";
}
}
}
}
ADDs = "";
if (m == 1) //生成SQL的添加语句
ADDs = "insert into " + TableName + " (" + tem_Field + ") values(" + tem_Value + ")";
if (m == 2) //生成SQL的修改语句
if (ID2 == "") //根据ID2参数,判断修改语句的条件
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID1 + "'";
else
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID2 + "'";
}
}
#endregion
请逐句解释下好吗?
#region 保存添加或修改的信息
/// <summary>
/// 保存添加或修改的信息.
/// </summary>
/// <param name="Sarr">数据表中的所有字段</param>
/// <param name="ID1">第一个字段值</param>
/// <param name="ID2">第二个字段值</param>
/// <param name="Contr">指定控件的数据集</param>
/// <param name="BoxName">要搜索的控件名称</param>
/// <param name="TableName">数据表名称</param>
/// <param name="n">控件的个数</param>
/// <param name="m">标识,用于判断是添加还是修改</param>
public void Part_SaveClass(string Sarr, string ID1, string ID2, Control.ControlCollection Contr, string BoxName, string TableName, int n, int m)
{
string tem_Field = "", tem_Value = "";
int p = 2;
if (m == 1){ //当m为1时,表示添加数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Field = "ID";
tem_Value = "'" + ID1 + "'";
p = 1;
}
else{
tem_Field = "Sut_id,ID";
tem_Value = "'" + ID1 + "','" + ID2 + "'";
}
}
else
if (m == 2){ //当m为2时,表示修改数据信息
if (ID1 != "" && ID2 == ""){ //根据参数值判断添加的字段
tem_Value = "ID='" + ID1 + "'";
p = 1;
}
else
tem_Value = "Sut_ID='" + ID1 + "',ID='" + ID2 + "'";
}
if (m > 0){ //生成部份添加、修改语句
string[] Parr = Sarr.Split(Convert.ToChar(','));
for (int i = p; i < n; i++)
{
string sID = BoxName + i.ToString(); //通过BoxName参数获取要进行操作的控件名称
foreach (Control C in Contr){ //遍历控件集中的相关控件
if (C.GetType().Name == "TextBox" | C.GetType().Name == "MaskedTextBox" | C.GetType().Name == "ComboBox")
if (C.Name == sID){ //如果在控件集中找到相应的组件
string Ctext = C.Text;
if (C.GetType().Name == "MaskedTextBox") //如果当前是MaskedTextBox控件
Ctext = Date_Format(C.Text); //对当前控件的值进行格式化
if (m == 1){ //组合SQL语句中insert的相关语句
tem_Field = tem_Field + "," + Parr[i];
if (Ctext == "")
tem_Value = tem_Value + "," + "NULL";
else
tem_Value = tem_Value + "," + "'" + Ctext + "'";
}
if (m == 2)
{ //组合SQL语句中update的相关语句
if (Ctext=="")
tem_Value = tem_Value + "," + Parr[i] + "=NULL";
else
tem_Value = tem_Value + "," + Parr[i] + "='" + Ctext + "'";
}
}
}
}
ADDs = "";
if (m == 1) //生成SQL的添加语句
ADDs = "insert into " + TableName + " (" + tem_Field + ") values(" + tem_Value + ")";
if (m == 2) //生成SQL的修改语句
if (ID2 == "") //根据ID2参数,判断修改语句的条件
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID1 + "'";
else
ADDs = "update " + TableName + " set " + tem_Value + " where ID='" + ID2 + "'";
}
}
#endregion
请逐句解释下好吗?