C#机房重构-总结(三)

一,字符的限制

这个是两种限制,其实都是根据ascii码来写的,给大家加推荐一个博客,可以去了解一下更深层次的。

C#输入字符限制https://blog.csdn.net/u014067842/article/details/42236417,希望通过这个博客能给大家更多的启发。

            using System.Text.RegularExpressions;//引用之后才能用Regex
            Regex reg = new Regex(@"[^A-Za-z0-9]"); //限定输入范围是字母和数字
            if (reg.IsMatch(txtUserName.Text.ToString()))
            {
                MessageBox.Show("请输入有效数字!");
                txtUserName.Text = "";
                txtUserName.Focus();
                return;
            }
        

            Regex reg = new Regex(@"[^0-9]"); //只能输入数字
            if (reg.IsMatch(txtCardno.Text.ToString()))
            {
                MessageBox.Show("请输入有效数字!");
                txtCardno.Text = "";
                txtCardno.Focus();
                return;
            }

二,虚拟表DataTable

在机房重构中,有的需要将查询到的数据库中的元组作为返回值,在UI层显示,就需要用到DataTable来保存和返回查找结果。

大家看一下是怎么用的DataTable,也可以实例化 ,赋值也是可以的,下边是表头赋值。


DataTable dt=new DataTable("User");

//添加列,表头赋值

dt.Columns.Add("user_name",typeof(string));

dt.Columns.Add("user_password",typeof(string));

dt.Columns.Add("user_page",typeof(int));

这个是我D层的登录代码,下边的代码是虚拟表接收的数据库中查找到的数据。

        public DataTable selectUser(Entity.UserInfo UserInfo)//查询user表,当UI 的用户id和密码和数据中的对应相等时,返回这一元组
        {
            SQLHelper sqlHelper = new SQLHelper();   //实例化一个SQLHelper
            
            SqlParameter[] sqlParams = { new SqlParameter("@userID", UserInfo.UserID),
                                         new SqlParameter("@Password", UserInfo.PassWord) };
            string sql = @"SELECT * FROM [User_Info] WHERE  userID=@userID and PWD =@Password";
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }

将结果虚拟表返回到U层后,需要将虚拟表的值赋给datagridview控件。

 dataGridView1.DataSource = dt; //将查询的数据显示到表格中,dt定义的是虚拟表

//给dataGridView1表头赋值
 dataGridView1.Columns[0].HeaderText = "用户名";
 dataGridView1.Columns[1].HeaderText = "权限";
 dataGridView1.Columns[2].HeaderText = "登录日期";
 dataGridView1.Columns[3].HeaderText = "登录时间";
 dataGridView1.Columns[4].HeaderText = "退出日期";
 dataGridView1.Columns[5].HeaderText = "退出时间";
 dataGridView1.Columns[6].HeaderText = "机器名";
 dataGridView1.Columns[7].HeaderText = "状态";

这里有个一个问题,虚拟表能赋值表头,datagridview也是可是单独赋值给表头,所以,如果你在查找到你要的数据之后修改虚拟表的表头,就不用修改datagridview的了,至于为什么要修改表头,是因为数据库中都是英文表头,不方便咱们看,这么做更方便。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值