基于.net的应用开发技术-上机六

题目一:编写C#程序,统计硬盘某个目录下的abc.txt文件中单词的个数。提示:要用到字符串类中的分割字符串等函数。

源程序:

using System.Text.RegularExpressions;

namespace Exer06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
        }

        //统计单词个数
        private void button1_Click(object sender, EventArgs e)
        {
            Regex reg = new Regex("\\S+\\w+");
            string InputStr = richTextBox2.Text;
            int Count = reg.Split(InputStr).Count() - 1;
            richTextBox1.Text = "单词数为:" + Count.ToString();
        }

        //显示文件内容
        private void button2_Click(object sender, EventArgs e)
        {
            string file = textBox1.Text;
            if (!File.Exists(@file))
            {
                MessageBox.Show("文件不存在");
            }
            else {
                int a;
                FileStream fs=new FileStream(@file, FileMode.Open, FileAccess.Read);
                richTextBox2.Clear();
                a = fs.ReadByte();
                while (a != -1) {
                    richTextBox2.Text += ((char)a).ToString();
                    a = fs.ReadByte();
                }
                fs.Close();
            }
        }
    }
}

运行结果:   

题目二:编写一个重复文件的检测程序:程序可以实现重复文件检测(即将硬盘某个盘符下的重复文件以ListBox控件列表的形式显示出来,例如:有1.doc、2.doc、3.doc完全一样,则这三个应该放在同一个ListBox1控件中;而a.exe、b.exe完全一样,则放在另一个ListBox2控件中)(可由设计者自行设计分组)。

源程序:

namespace _6_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string path = @"D:\19200135105\test6-2";
        string checktxt = "";
        private void button1_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            ForeachFile(path, ref files);
            foreach (var f in files)
            {
                try
                {
                    string txt = "";
                    using (StreamReader sr = new StreamReader(f.ToString()))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            txt += line + "\n";
                            Console.WriteLine(line);
                        }
                    }
                    if (checktxt == "")
                    {
                        listBox1.Items.Add(f.ToString());
                        checktxt = txt;
                    }
                    else if (txt == checktxt)
                    {
                        listBox1.Items.Add(f.ToString());
                    }
                    else
                    {
                        listBox2.Items.Add(f.ToString());
                    }
                }
                catch
                {
                    Console.WriteLine("The file could not be read:");
                }
            }
        }
        public static void ForeachFile(string filePathByForeach, ref List<string> result)

        {
            DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
            FileInfo[] file = theFolder.GetFiles();//获取所在目录的文件

            foreach (FileInfo fileItem in file) //遍历文件
            {
                result.Add(fileItem.DirectoryName + "\\" + fileItem.Name);
            }
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                ForeachFile(NextFolder.FullName, ref result);
            }
        }
    }
}

运行结果:

题目三:编程实现个人通讯录信息的添加、删除、查找和更新功能。

要求数据库可自行选取,采用编程方式实现,界面自行设计。

源程序:

注册:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_3
{
    public partial class register : Form
    {
        public register()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string name = textBox1.Text.Trim();
                string pwd = textBox2.Text.Trim();
                //这里填写自己电脑对应的信息
                string sqlcon = @"Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Integrated Security=SSPI";
                SqlConnection sc = new SqlConnection(sqlcon);
                string sql = "INSERT INTO userInfo (id, username, pwd) VALUES (" + new Random().Next(99) + ", '" + textBox1.Text + "', '" + textBox2.Text + "')";
                SqlCommand cmd = new SqlCommand(sql, sc);
                SqlParameter sqlParameter = new SqlParameter("@username", textBox1.Text);
                cmd.Parameters.Add(sqlParameter);
                sqlParameter = new SqlParameter("@pwd", textBox2.Text);
                cmd.Parameters.Add(sqlParameter);
                sc.Open();
                cmd.ExecuteNonQuery();
                sc.Close();
                MessageBox.Show("注册成功,您现在可以登录了");
                if (textBox1.Text.Trim() == "")
                {
                    MessageBox.Show("请输入用户名");
                }
                if (textBox2.Text.Trim() == "")
                {
                    MessageBox.Show("请输入密码");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            login f = new login();
            this.Hide();
            f.ShowDialog();
            Application.ExitThread();
        }
    }
}

登录:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_3
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }
        //这里填写自己电脑的对应信息
        static String connStr = "Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Persist Security Info=True;User ID=XX;Password=X";

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql2 = "SELECT count(*) count FROM userInfo where username='" + textBox1.Text + "' and pwd = '" + textBox2.Text + "'";
            SqlCommand cmd = new SqlCommand(sql2, conn);
            int i = (int)cmd.ExecuteScalar();
            if (i > 0)
            {
                phoneInfo message = new phoneInfo();
                message.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
            phoneInfo f3 = new phoneInfo();
            this.Hide();
            f3.ShowDialog();
            Application.ExitThread();
        }
    }
}

 显示通讯录信息:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_3
{
    public partial class phoneInfo : Form
    {
        //这里填写自己电脑的对应信息
        static String connStr = "Data Source=DESKTOP-XXXXXXX;Initial Catalog=test;Persist Security Info=True;User ID=XX;Password=X";
        SqlCommand cmd;
        int RowIndex = -1;

        public phoneInfo()
        {
            InitializeComponent();
            Load();
        }

        //删除
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    RowIndex = i;
                }
            }
            if (RowIndex == -1)
            {
                MessageBox.Show("请选择学生再删除!");
            }
            else
            {
                DialogResult result = MessageBox.Show("确定删除" + dataGridView1.Rows[RowIndex].Cells[1].Value + "的信息?", "删除", MessageBoxButtons.OKCancel);
                int pid = 0;
                if (result == DialogResult.OK)
                {
                    SqlConnection conn = new SqlConnection(connStr);
                    conn.Open();
                    pid = Convert.ToInt32(dataGridView1.Rows[RowIndex].Cells[0].Value);
                    Console.WriteLine(pid);
                    string sql2 = "delete from phone where pid =" + pid;
                    cmd = new SqlCommand(sql2, conn);
                    cmd.ExecuteNonQuery();
                    RowIndex = -1;
                }
                ManagePhone_Load(sender, e);
            }
        }
       //修改
        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    RowIndex = i;
                }
            }
            if (RowIndex == -1)
            {
                MessageBox.Show("请选择学生再修改!");
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                phoneInfo manage = this;
                updatePhone phone = new updatePhone(dataGridView1.Rows[RowIndex].Cells, conn, manage);
                phone.Show();
                ManagePhone_Load(sender, e);
            }
        }

        //新增
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            phoneInfo manage = this;
            addPhone addStudent = new addPhone(conn, manage);
            addStudent.Show();
            ManagePhone_Load(sender, e);
        }

         private void ManagePhone_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            String sql = "select * from phone";
            SqlCommand cmd = new SqlCommand(connStr);
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }
        public new void Load()
        {
            this.ManagePhone_Load(null, null);
        }
    }
}

修改:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_3
{
    public partial class updatePhone : Form
    {
        private DataGridViewCellCollection row = null;
        private SqlConnection conn = null;
        private phoneInfo manage = null;
        private int status = 0;

        public updatePhone(DataGridViewCellCollection row, SqlConnection conn, phoneInfo manage)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            this.conn = conn;
            this.row = row;
            this.textBox1.Text = Convert.ToString(row[0].Value);
            this.textBox2.Text = Convert.ToString(row[1].Value);
            this.textBox3.Text = Convert.ToString(row[2].Value);
            this.textBox4.Text = Convert.ToString(row[3].Value);
            int pid = Convert.ToInt32(row[0].Value);
            this.manage = manage;
        }

        public int getStatus()
        {
            return status;
        }

        //修改
        private void button1_Click(object sender, EventArgs e)
        {
            int pid = Convert.ToInt32(this.textBox1.Text);
            Console.WriteLine(pid);
            string sql2 = "update phone set pname = '" + textBox2.Text + "',pnumber = '" + textBox3.Text + "',pemail = '" + textBox4.Text + "' where pid =" + pid;
            SqlCommand cmd = new SqlCommand(sql2, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.Write(sql2);
            MessageBox.Show("修改成功!");
            this.status = -1;
            manage.Load();
            this.Close();
        }
    }
}

新增:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6_3
{
    public partial class addPhone : Form
    {
        private SqlConnection conn;
        private phoneInfo manage;

        public addPhone(SqlConnection conn, phoneInfo manage)
        {
            this.conn = conn;
            this.manage = manage;
            InitializeComponent();
        }

        public addPhone()
        {
            InitializeComponent();
        }

        //添加
        private void button1_Click(object sender, EventArgs e)
        {
            string sql2 = "insert into phone (pid, pname, pnumber, pemail) VALUES (" + new Random().Next(999) + ", '" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "')";
            SqlCommand cmd = new SqlCommand(sql2, conn);
            MessageBox.Show(sql2);
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.Write(sql2);
            MessageBox.Show("添加成功!");
            manage.Load();
            this.Close();
        }
    }
}

运行结果:

用户注册:

对应新增一条数据:

登录:

添加一条信息:

对应界面变化:

对应数据库:

修改(修改邮箱):

对应界面变化:

删除:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值