当前位置:编程学习 > C#/ASP.NET >>

C#程序语言解释

有两段语言,麻烦懂的人把帮我逐行解释一下,每段代码实现了什么功能,做了什么事情。我不太懂,谢谢啦。

 

{

    public partial class user : Form

    {

        public user()

        {

            InitializeComponent();

        }

        private void AcceptButton_Click(object sender, EventArgs e)

        {

            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")

            {

                MessageBox.Show("用户名和密码不能为空!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;

            }

                SqlConnection conn = new

                SqlConnection("server=.;database=students;Trusted_Connection=yes;");

                StringBuilder sql = new StringBuilder();

                sql.AppendFormat("select count(*) from 管理员信息 where 用户名字='{0}' and 用户密码='{1}' and 用户身份='{2}'", textBox1.Text.Trim(), textBox2.Text.Trim(),comboBox1.Text.Trim());

                SqlCommand cmd = new SqlCommand(sql.ToString(), conn);

                conn.Open();

                int result = Convert.ToInt32(cmd.ExecuteScalar());

                conn.Close();

                if (comboBox1.SelectedIndex == 0)

                {

                    if (result == 1)

                    {

                        stunews f = new stunews();

                        f.toolStripStatusLabel3.Text =textBox1.Text.Trim();

                        f.Show();

                        this.Hide();

                    }

                    else

                    {

                        MessageBox.Show("失败");

                        return;

                    }

                }

                else if (comboBox1.SelectedIndex == 1)

                {

                    if (result == 1)

                    {

                        Main a = new Main();

                        a.Show();

                        this.Hide();

                    }

                    else

                    {

                        MessageBox.Show("失败");

                    }

                }

                else if (comboBox1.SelectedIndex == 2)

                {

                    Main a = new Main();

                    a.Show();

                    this.Hide();

                }

            }

        private void Button2_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

        private void user_Load(object sender, EventArgs e)

        {

        }

    }

}

 

 

还有一段

{
    public partial class 学生管理 : Form
    {
        public 学生管理()
        {
            InitializeComponent();
        }
       
        private void 学生管理_Load(object sender, EventArgs e)
        {
        }
        //查询
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new
                 SqlConnection("server=.;database=students;Trusted_Connection=yes;");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
                string sql = "select * from 学生信息 where 学号 like '%" + textBox1.Text+"%'";
                            
                cmd.CommandText = sql;
                cmd.Connection = conn;
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                sda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
      
        }
       
        //添加
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=students;Trusted_Connection=yes;");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            string mycomm = "insert into  学生信息(学号,姓名,性别,出生日期,籍贯,家庭住址) values(" + "'" + textBox1.Text + "'," + "'" + textBox2.Text + "','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"')";
            cmd.CommandText = mycomm;
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            MessageBox.Show("添加成功!");
        }
        //修改
        private void toolStripButton3_Click(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection("server=.;database=students;Trusted_Connection=yes;");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            string sql = "update 学生信息  set 姓名= '" + textBox2.Text.Trim() + "',性别='" + textBox3.Text.Trim() + "',出生日期='" + textBox4.Text.Trim() + "',籍贯='" + textBox5.Text.Trim() + "',家庭住址='" + textBox6.Text.Trim() + "' where 学号='" + textBox1.Text.Trim() + "'";
            cmd.CommandText = sql;
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            MessageBox.Show("修改成功");
           
        }
        //删除
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=students;Trusted_Connection=yes;");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            string mycomm = "delete  from 学生信息 where 姓名 like '%" + textBox2.Text + "%'";
            cmd.CommandText = mycomm;
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            MessageBox.Show("删除成功!");
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
      
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Main m = new Main();
            m.Show();
            this.Hide();
        }
      
    }
}

追问:是个管理系统的代码,可是我想知道每行代码的意思,执行了什么功能,数据库怎么连接的。你能帮忙解析一下吗?
答案:
这是一个学生管理信息系统的代码,显示登录界面的代码,接下来就是系统界面的代码,能实现添加、修改、删除、查询等功能

饿!这个代码还用别人解释吗?你不学这个吗?学了不可能不知道吧!都是很简单的啊!如果没解决我帮你下!解决了就算了!不浪费那时间了。。。

顶楼上de 

上一个:c#UDP打洞
下一个:C#读取二进制文件

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,