使用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();
            }

        }

       








      

    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值