使用C#开发数据库应用系统 第二章

父窗口:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void 添加信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 tu = new Form2();

            tu.MdiParent = this;           
          
            tu.Show();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
 
 
 
子窗口:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        string ent = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
      
        public string run = string.Empty;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //for (int i = 1; i <= 10; i++)
            //{
            //    this.comboBox1年级.Items.Add(i);
            //}
            comboBox1年级.Items.Add("请选择");
            comboBox1年级.SelectedIndex = 0;
            BindGread();

           
        }
       
        public bool BindGread()
        {
           SqlConnection conn = new SqlConnection(ent);
           Form2 per = new Form2();
            try
            {
                string sql = "select * from dbo.Grade";

                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader MyReder = comm.ExecuteReader();
                while (MyReder.Read())
                {          //获取年级名称
                    string gradename = MyReder["GradeName"].ToString();
                    //将年级名称添加到组合框中
                    comboBox1年级.Items.Add(gradename);
                }
                MyReder.Close();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show("系统发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return false;
            }
            finally
            {
                per.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 保存_Click(object sender, EventArgs e)
        {
            if(shout()){
               // MessageBox.Show("是否保存","操作提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                setdata();
            }
          
        }

        public bool shout()
           {
        if (this.id.Text.Trim().Equals(string.Empty))
        {
            MessageBox.Show("请输入学号", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.id.Focus();
            return false;
        }else if(this.pwd.Text.Trim().Equals(string.Empty))
        {
            MessageBox.Show("请输入密码", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.pwd.Focus();
            return false;
        }else if(this.pwds.Text.Trim().Equals(string.Empty))
        {   
            MessageBox.Show("请输入确认密码", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.pwds.Focus();
            return false;
        }else if(this.name.Text.Trim().Equals(string.Empty))
        {
             MessageBox.Show("请输入姓名", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.name.Focus();
            return false;
        }else if(this.comboBox1年级.Text.Trim().Equals(string.Empty))
        {
             MessageBox.Show("请输入年级", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.comboBox1年级.Focus();
            return false;
        }else if(this.phone.Text.Trim().Equals(string.Empty))
        {
             MessageBox.Show("请输入电话", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.id.Focus();
            return false;
        }else if(this.ee.Text.Trim().Equals(string.Empty))
        {
            string ru = "请输入Email,必须包含@";
             MessageBox.Show(ru ,"操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            this.id.Focus();
            return false;
        }
        else
        {
         return true;
        } 

    }


        public void setdata()
        {
            bool success = true;
            SqlConnection conn = new SqlConnection(ent);
            string id = this.id.Text;
            string pwd = this.pwd.Text;
            string pwds = this.pwds.Text;
            string name = this.name.Text;
            string comboBox1年级 = this.comboBox1年级.Text;
            string phone = this.phone.Text;
            string address = this.address.Text;
            string time = this.time.Text;
            string ee = this.ee.Text;
            string sex = "";
            if (sex1.Checked == true)
            {
                sex = "男";
            }
            else if (sex2.Checked == true)
            {
                sex = "女";
            }

            DateTime date = this.time.Value; //出生日期
            string birthday = string.Format("{0}-{1}-{2}", date.Year, date.Month, date.Day);
            try
            {
                conn.Open();
                //构建按年级名称查询年级编号的Sql语句
                string sqlname = string.Format(" select * from Student where GradeName='{0}'");

                //创建Command对象
                SqlCommand command = new SqlCommand(sqlname, conn);

                //查询获得年级编号
                int gradeld = (int)command.ExecuteScalar();

                //查询插入学生记录的SQL语句
                StringBuilder sql = new StringBuilder();
                sql.AppendLine("insert into Student (LoginPwd,StudentName,Gender,GradeId,Phone,Address,Birthday,Email) ");
                sql.AppendFormat("VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", pwd, name, sex, comboBox1年级, phone, address, birthday, ee);

                //创建command对象
                command = new SqlCommand(sql.ToString(), conn);

                //打开数据库连接
                conn.Open();

                //执行命令
                int result = command.ExecuteNonQuery();

                //根据操作结果给出提示信息
                if (result == 1)
                {
                    string SqlNo = "select @@IDENTITY from  dbo.Student";
                    command.CommandText = SqlNo;
                    int StudentNo = Convert.ToInt32(command.ExecuteScalar()); //学号
                    this.id.Text = StudentNo.ToString();
                }
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                conn.Close();
            }

        }

       








      

    }
}



展开阅读全文

没有更多推荐了,返回首页