C#实验报告上机五

题目一:编写C#程序:在D盘根目录下创建一个以自己的学号命名的目录,然后在该目录下创建一个(自己学号.txt)的文本文件,在该文件中写入自己的学号、姓名、学校、专业、英语成绩等信息;然后再读出文件中的信息显示到屏幕上。

源程序:

namespace Demo1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }


        private void button2_Click(object sender, EventArgs e)

        {

            string path = "d:\\19200135226";

            string fileName = "19200135226.txt";

            if (!System.IO.Directory.Exists(path))

            {

                System.IO.Directory.CreateDirectory(path);

            }

            System.IO.File.AppendText(System.IO.Path.Combine(path, fileName)).Close();

            string text = "学号:123456789\n姓名:摆烂\n学校:苏州大学\n专业:计算机科学与技术\nC#成绩:100\nCSDN博主:书启秋枫";

            System.IO.File.WriteAllText(@"d:\\19200135226\\19200135226.txt", text);

            MessageBox.Show("写入成功!", "信息注入");

            richTextBox1.Text = text;

        }

    }

}

运行结果: 

题目二:创建一个学生信息系统登录界面,有输入用户名、密码功能,以及具有注册的功能。在学生初次使用时,可进行注册。注册成功后,再次登录可进入系统管理界面,在系统管理界面可进行学生注册信息的查询、修改操作。

该系统提供学生的注册、及管理信息管理功能页面可由学生自行设计。要求采用SQL Server数据库,通过编程方式实现。

源程序:

public partial class Main : Form

    {

        public Main()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }



        private void registBtn_Click(object sender, EventArgs e)

        {

            Register register = new Register();

            register.Show();

        }



        private void loginBtn_Click(object sender, EventArgs e)

        {

            Login login = new Login();

            login.Show();

        }



        private void Main_Load(object sender, EventArgs e)

        {

            // TODO: 这行代码将数据加载到表“s_TDataSet1.users”中。您可以根据需要移动或移除它。

            this.usersTableAdapter.Fill(this.s_TDataSet1.users);



        }

    }

 

public partial class Register : Form

    {

        public Register()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }



        static String connStr = "Data Source=DESKT******62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*******";



        private void submit_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            string sql2 = "INSERT INTO users (user_id, username, password) VALUES ("+ new Random().Next(99999) + ", '"+username.Text+"', '"+password.Text+"')";

            SqlCommand cmd = new SqlCommand(sql2, conn);

            int i = cmd.ExecuteNonQuery();

            if (i == 1)

            {

                MessageBox.Show("注册成功!");

                this.Close();

            }

            else

            {

                MessageBox.Show("注册失败!");

            }

        }

    }

 public partial class Login : Form

    {

        public Login()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }



        static String connStr = "Data Source=DESKT*****V62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*******";



        private void submit_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            string sql2 = "SELECT count(*) count FROM users where username='"+username.Text+"' and password = '"+password.Text+"'";

            SqlCommand cmd = new SqlCommand(sql2, conn);

            int i = (int)cmd.ExecuteScalar();

            if (i > 0)

            {

                MessageBox.Show("登录成功!");

                ManageStudent manage = new ManageStudent();

                manage.Show();

                this.Close();

            }

            else

            {

                MessageBox.Show("登录失败!");

            }

        }

    }

 public partial class AddStudent : Form

    {

        public AddStudent()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }

        

        private SqlConnection conn;

        private ManageStudent manage;

        public AddStudent(SqlConnection conn, ManageStudent manage)

        {

            this.conn = conn;

            this.manage = manage;

            InitializeComponent();



        }

        private void button1_Click(object sender, EventArgs e)

        {

            string sql2 = "insert into student (Sno, Sname, Ssex, Sage , Sdept) VALUES ('"+ sno.Text + "', '"+Sname.Text+"', '"+Ssex.Text+"', "+Sage.Text+", '"+Sdept.Text+"')";

            SqlCommand cmd = new SqlCommand(sql2, conn);

            MessageBox.Show(sql2);

            cmd.ExecuteNonQuery();

            conn.Close();

            Console.Write(sql2);

            MessageBox.Show("添加成功!");

            manage.Load();

            this.Close();

        }

    }

 public partial class UpdateStudent : Form

    {

        public UpdateStudent()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

        }


        private void button1_Click_1(object sender, EventArgs e)

        {

            int Sno = Convert.ToInt32(this.sno.Text);

            Console.WriteLine(Sno);

            string sql2 = "update student set Sname = '" + Sname.Text + "',Ssex = '" + Ssex.Text + "',Sage = '" + Sage.Text + "',Sdept = '" + Sdept.Text + "' where Sno =" + Sno;

            SqlCommand cmd = new SqlCommand(sql2, conn);

            cmd.ExecuteNonQuery();

            conn.Close();

            Console.Write(sql2);

            MessageBox.Show("修改成功!");

            this.status = -1;

            manage.Load();

            this.Close(); 

        }

    }

public partial class ManageStudent : Form

    {

        static String  connStr = "Data Source=DESKT****UV62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*******";

        SqlCommand cmd;

        int RowIndex = -1;

        public ManageStudent()

        {

            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;

            Load();

        }



        public void Load()

        {

            this.ManageStudent_Load(null,null);

        }

        private void ManageStudent_Load(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            String sql = "SELECT Sno,replace(Sname,' ','') Sname,Ssex,Sage,replace(Sdept,' ','') Sdept from student";

            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();

        }



        private void button4_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 Sno = 0;

            if (result == DialogResult.OK)

            {

                SqlConnection conn = new SqlConnection(connStr);

                conn.Open();

                Sno = Convert.ToInt32(dataGridView1.Rows[RowIndex].Cells[0].Value);

                Console.WriteLine(Sno);

                string sql2 = "delete from student where Sno =" + Sno;

                cmd = new SqlCommand(sql2, conn);

                cmd.ExecuteNonQuery();

                RowIndex = -1;

            }

            ManageStudent_Load(sender, e);

            }

        }



        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

            {

                SqlConnection conn = new SqlConnection(connStr);

                conn.Open();

                ManageStudent manage = this;

                UpdateStudent updateStudent = new UpdateStudent(dataGridView1.Rows[RowIndex].Cells,conn, manage);

                updateStudent.Show();

                ManageStudent_Load(sender, e);

            }



        }



        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            ManageStudent manage = this;

            AddStudent addStudent = new AddStudent(conn, manage);

            addStudent.Show();

            ManageStudent_Load(sender, e);

        }



        private void button3_Click(object sender, EventArgs e)

        {

            Main main = new Main();

            main.Show();

            this.Close();

        }



        private void ManageStudent_Load_2(object sender, EventArgs e)

        {

            this.studentTableAdapter.Fill(this.s_TDataSet.Student);



        }

    }

猜你喜欢

转载自blog.csdn.net/qq_45037155/article/details/124744645