C# 学习笔记 三层架构系列(控件一)

时间:2023-03-08 22:04:32

下面是我两周的学习总结:这是我写给自己的,如果哪位朋友有幸看到这篇文章就是缘分。如果所说的内容不对,就请纠正。勿喷!!!

想要将两周的学习知识通过文字、通过代码、通过图片储备起来,以防自己那天思维短路可以再次方便自己回顾。

下面第一节我将要学习三层架构基础东西

C#结构分为三层:1、UI层:就是Winform的框架集,方便用户对控件的操作与对控件的事件处理等。

         2、BLL层:用于自己是JAVA出身,做东西喜欢和JAVA比较,我认为这层就是java的接口或service,用来声明void和有返回值的接口。用于在UI层调用相应的接口,并在DAL层实现所要执行的操作。

3、DAL层:对我来说就是JAVA中的IMPL实现,有返回值方法和无返回值方法等。也可在这里做事务处理等(后期再聊)。

百度:--------------

3层架构是一种“客户端-服务器”架构,在此架构中用户接口,商业逻辑,数据保存以及数据访问被设计为独立的模块。主要有3个层面,第一层(表现层,GUI层),第二层(商业对象,商业逻辑层),第三层(数据访问层)。这些层可以单独开发,单独测试。

----------------------

下面将做的是对控件的处理:

1、C# 学习笔记 三层架构系列(控件一) 拖入三个复选框在页面上,对name进行简单的命名

2、下载对三个复选框做状态的更改 ‘选中、非选中’。

首先在加载窗口中定义

  ArrayList tab1_ArrayList_CheckBox = new ArrayList();

  tab1_ArrayList_CheckBox.Add(checkBox1_jjxx);
  tab1_ArrayList_CheckBox.Add(checkBox2_kwjlj);
  tab1_ArrayList_CheckBox.Add(checkBox3_jg);

  

  // 通过[QueryFrmSetControlINFO]存储过程 查询出Operator所有数据 CheckStutas,BaseName,TabIndex
  DataSet ds = setControl_BLL.QueryFrmSetControlINFO();
  GetTabArrayListCheckBox(ds, tab1_ArrayList_CheckBox);

  

  

  //GetTabArrayListCheckBox(ds, tab1_ArrayList_CheckBox);
  private void GetTabArrayListCheckBox(DataSet ds, ArrayList tabArrayList)
  {
      // 遍历CheckBox复选框集合
    for (var i = 0; i < tabArrayList.Count; i++)
      {
        CheckBox CheckBox = (CheckBox)tabArrayList[i];
        // 遍历Operator表中的记录 进行列名称比较

          for (var j = 0; j < ds.Tables[0].Rows.Count; j++)
          {
            // 比较页面上字段与数据库属性是否相同

            if (CheckBox.Text == ds.Tables[0].Rows[j]["BaseName"].ToString())
                {
                  Boolean checkBox_Checked_Flag = ds.Tables[0].Rows[j]["CheckStutas"].ToString() == "True" ? true : false;
                  CheckBox.Checked = checkBox_Checked_Flag;
                  break;
                }
          }
  }
}

这段代码就意味着数据库中会有对应的数据表存储的三个复选框中的数据,会保存三个复选框的数值和选中状态。

C# 学习笔记 三层架构系列(控件一)

之后会通过窗体加载事件将三个复选框的checked的状态通过名称做比较,如果true代表选中。

--------------

下面将通过按钮保存三个复选框的数据状态等。

通过按钮单击事件:保存数据

private void button_OK_Click(object sender, EventArgs e)
{
//Tab1

//int index = 1;
//int index = int.Parse(tabPage1.Name.Substring(tabPage1.Text.Length - 1));
int index1 = int.Parse(tabPage1.Name.Substring(tabPage1.Name.Length - 1));
UpdateOperatorCheckBoxCheckStutas(tab1_ArrayList_CheckBox, index1);

}

private void UpdateOperatorCheckBoxCheckStutas(ArrayList tab_ArrayList_CheckBox, int index)
{
int Check_Stutas;
string Check_Title;
// 遍历CheckBox复选框集合
for (var i = 0; i < tab_ArrayList_CheckBox.Count; i++)
{
CheckBox CheckBox = (CheckBox)tab_ArrayList_CheckBox[i];
if (CheckBox.Checked)
{
Check_Stutas = 1;
Check_Title = CheckBox.Text;
}
else
{
Check_Stutas = 0;
Check_Title = CheckBox.Text;
}
//更新Operator表中CheckStatus,BaseName两个字段
UpdateCheckBox(Check_Stutas, Check_Title, index);
}
}

。。。。。。。。。。。。。。。。割了  上面是对CheckBox的加载和处理

第二内容:是对groupbox的处理

1、如通过按钮来控制groupbox容器的显示与否。

数据库中数据及状态:

C# 学习笔记 三层架构系列(控件一)

ArrayList arrayList1_GroupBox = new ArrayList();

private void Frmsetting_Load(object sender, EventArgs e)
{

// 通过[GetFrmSettingINFO]存储过程 查询出Operator所有数据
DataSet ds = frmsetting_BLL.Frmsetting_Operator_BLL();

// 将GroupBox加载到ArrayList集合
arrayList1_GroupBox.Add(groupBox2_jjxx);
arrayList1_GroupBox.Add(groupBox1_kwjlj);
arrayList1_GroupBox.Add(groupBox4_jj);
// 传参ArrayList 进行遍历
GetArrayList_GroupBox(ds, arrayList1_GroupBox);

// 通过[GetFrmSettingINFO]存储过程 查询出Operator所有数据
// 传参ArrayList 进行遍历
private void GetArrayList_GroupBox(DataSet ds, ArrayList alist)
{
// 遍历ArrayList中存储的个数
for (int i = 0; i < alist.Count; i++)
{
GroupBox groupBox = (GroupBox)alist[i];
// 遍历数据库表Operator 中数据的个数
for (var j = 0; j < ds.Tables[0].Rows.Count; j++)
{
// 名称和数据库表Operator表中属性做比较
if (groupBox.Text == ds.Tables[0].Rows[j]["BaseName"].ToString())
{
Boolean groupBox_Visible = ds.Tables[0].Rows[j]["CheckStutas"].ToString() == "True" ? true : false;
groupBox.Enabled = groupBox_Visible;
break;
}
}
}
}

在数据库中查询groupbox的属性  之后通过判断来决定是否groupbox的容器控件是否可用。

....................................................割了。。。。。。。。。。。。

--------------------------------------下面是对textbox控件的梳理

在页面上拖动几个textbox控件在界面上

C# 学习笔记 三层架构系列(控件一)

修改名称,之后通过名称来保存数据到数据库中

UI层代码:

private void button_CaoZuo_ok_Click(object sender, EventArgs e)
{
// tabpage1 操作
string caoZuoZhe = txt_CZZ.Text;
string jianCeDiDian = txt_JCDD.Text;
string caiJiShiJian = txt_CJSJ.Text;

if (caoZuoZhe != "" || jianCeDiDian != "" || caiJiShiJian != "")
{
frmsetting_BLL.Insert_Frmsetting_BaseInfo(caoZuoZhe, jianCeDiDian, caiJiShiJian);
}

}

BLL层:

public void Insert_Frmsetting_BaseInfo(string caoZuoZhe, string jianCeDiDian, string caiJiShiJian)
{
  Frmsetting_DAL.InsertFrmSetting_BaseInfo_INFO(caoZuoZhe, jianCeDiDian, caiJiShiJian);
}

DAL层:

#region "插入BaseInfo信息"
/// <summary>
///插入BaseInfo表数据
/// </summary>
/// <param name="caoZuoZhe">Operatorer</param>
/// <param name="caoZuoZhe">Address</param>
/// <param name="caoZuoZhe">CollectionTime</param>
public static void InsertFrmSetting_BaseInfo_INFO(string caoZuoZhe, string jianCeDiDian, string caiJiShiJian)
{
string sqlCommand = "InsertFrmSettingBaseInfo";
SqlParameter[] param ={
new SqlParameter("@caoZuoZhe",SqlDbType.VarChar,50),
new SqlParameter("@jianCeDiDian",SqlDbType.VarChar,50),
new SqlParameter("@caiJiShiJian",SqlDbType.VarChar,50)
};
param[0].Value = caoZuoZhe;
param[1].Value = jianCeDiDian;
param[2].Value = caiJiShiJian;

SqlHelper.ExecuteNonQuery(GHGD.Conn.Conn.SqlConn, CommandType.StoredProcedure, sqlCommand, param);
}
#endregion

以上代码就是c# 三层架构的简单代码。

。。。。。。。。。。。。。。。。。。割了

-------------------------------------下面是对ComboBox 下拉框控件的操作:

对下拉框的操作有两种方式:

一种是在其通过手动方式添加ComboBox 下拉项;另一种是通过字典表中数据之后通过限制条件添加ComboBox 下拉项中。

我记录的是通过字典表加入添加ComboBox 下拉项数据:上代码

数据库数据:

C# 学习笔记 三层架构系列(控件一)

窗体加载事件:

// tabpage2
// 温度
string tabpage2_str = "温度";
DataSet tabpage2_dsComboBox = dic_Bll.GetDic(tabpage2_str);
DataTable dt = new DataTable();
dt = tabpage2_dsComboBox.Tables[0];
comboBox6_wendu.DataSource = dt;
comboBox6_wendu.DisplayMember = "DICName";
comboBox6_wendu.ValueMember = "ID";

// 湿度
string tabpage2_str2 = "湿度";
DataSet tabpage2_dsComboBox2 = dic_Bll.GetDic(tabpage2_str2);
DataTable dt2 = new DataTable();
dt2 = tabpage2_dsComboBox2.Tables[0];
comboBox7_shidu.DataSource = dt2;
comboBox7_shidu.DisplayMember = "DICName";
comboBox7_shidu.ValueMember = "ID";

以上代码是将字典表数据加载到ComboBox下拉框中:

下面代码是保存ComboBox数据到数据库中:

UI:

private void button4_YingJian_ok_Click(object sender, EventArgs e)
{
int wendu = comboBox6_wendu.SelectedIndex;
int shidu = comboBox7_shidu.SelectedIndex;
// tabpage2 数据
frmsetting_BLL.Insert_Frmsetting_SensorInfo(wendu, shidu);
}

BLL:

public void Insert_Frmsetting_SensorInfo(int wendu, int shidu)
{
Frmsetting_DAL.Insert_Frmsetting_SensorInfo(wendu, shidu);
}

DAL:

#region "插入Sensor表信息"
/// <summary>
///插入Sensor表信息
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
public static void Insert_Frmsetting_SensorInfo(int wendu, int shidu)
{
string sqlCommand = "Insert_Frmsetting_SensorInfo";

SqlParameter[] param ={
new SqlParameter("@wendu",SqlDbType.Int),
new SqlParameter("@shidu",SqlDbType.Int)
};
param[0].Value = wendu;
param[1].Value = shidu;

SqlHelper.ExecuteNonQuery(GHGD.Conn.Conn.SqlConn, CommandType.StoredProcedure, sqlCommand, param);
}
#endregion

..................。。。。。。。。。。。。。。。。割了

以上是对几个简单的控件的使用。